From 090528af87a41dc411af4139389dbe3792c4fffe Mon Sep 17 00:00:00 2001 From: bouni Date: Tue, 14 May 2019 09:18:25 +0200 Subject: [PATCH 1/3] Added support for sensor other than temperature and humidity --- homeassistant/components/spaceapi/__init__.py | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/spaceapi/__init__.py b/homeassistant/components/spaceapi/__init__.py index 5431cd6260c1d8..ee1a9ed141aae7 100644 --- a/homeassistant/components/spaceapi/__init__.py +++ b/homeassistant/components/spaceapi/__init__.py @@ -28,6 +28,7 @@ ATTR_UNIT = 'unit' ATTR_URL = 'url' ATTR_VALUE = 'value' +ATTR_SENSOR_LOCATION = 'location' CONF_CONTACT = 'contact' CONF_HUMIDITY = 'humidity' @@ -72,9 +73,10 @@ vol.Inclusive(CONF_ICON_OPEN, CONF_ICONS): cv.url, }, required=False) -SENSOR_SCHEMA = vol.Schema( - {vol.In(SENSOR_TYPES): [cv.entity_id]} -) +SENSOR_SCHEMA = vol.Schema({ + vol.In(SENSOR_TYPES): [cv.entity_id], + cv.string: [cv.entity_id] +}) CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ @@ -105,6 +107,25 @@ class APISpaceApiView(HomeAssistantView): url = URL_API_SPACEAPI name = 'api:spaceapi' + @staticmethod + def get_sensor_data(hass, spaceapi, sensor): + """get necessary data from senor""" + sensor_state = hass.states.get(sensor) + if not sensor_state: + return None + sensor_data = { + ATTR_NAME: sensor_state.name, + ATTR_VALUE: sensor_state.state + } + if ATTR_SENSOR_LOCATION in sensor_state.attributes: + sensor_data[ATTR_LOCATION] = sensor_state.attributes[ATTR_SENSOR_LOCATION] + else: + sensor_data[ATTR_LOCATION] = spaceapi[CONF_SPACE] + # Some sensors don't have a unit of measurement + if ATTR_UNIT_OF_MEASUREMENT in sensor_state.attributes: + sensor_data[ATTR_UNIT] = sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] + return sensor_data + @ha.callback def get(self, request): """Get SpaceAPI data.""" @@ -152,17 +173,10 @@ def get(self, request): if is_sensors is not None: sensors = {} for sensor_type in is_sensors: + sensors[sensor_type] = [] sensors[sensor_type] = [] for sensor in spaceapi['sensors'][sensor_type]: - sensor_state = hass.states.get(sensor) - unit = sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] - value = sensor_state.state - sensor_data = { - ATTR_LOCATION: spaceapi[CONF_SPACE], - ATTR_NAME: sensor_state.name, - ATTR_UNIT: unit, - ATTR_VALUE: value, - } + sensor_data = self.get_sensor_data(hass, spaceapi, sensor) sensors[sensor_type].append(sensor_data) data[ATTR_SENSORS] = sensors From b699ac971021c180a329116839f687ab920dab06 Mon Sep 17 00:00:00 2001 From: bouni Date: Tue, 14 May 2019 12:35:17 +0200 Subject: [PATCH 2/3] fixed lint errors --- homeassistant/components/spaceapi/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/spaceapi/__init__.py b/homeassistant/components/spaceapi/__init__.py index ee1a9ed141aae7..ceb757c8a98f56 100644 --- a/homeassistant/components/spaceapi/__init__.py +++ b/homeassistant/components/spaceapi/__init__.py @@ -109,7 +109,7 @@ class APISpaceApiView(HomeAssistantView): @staticmethod def get_sensor_data(hass, spaceapi, sensor): - """get necessary data from senor""" + """get necessary data from senor""" sensor_state = hass.states.get(sensor) if not sensor_state: return None @@ -118,12 +118,14 @@ def get_sensor_data(hass, spaceapi, sensor): ATTR_VALUE: sensor_state.state } if ATTR_SENSOR_LOCATION in sensor_state.attributes: - sensor_data[ATTR_LOCATION] = sensor_state.attributes[ATTR_SENSOR_LOCATION] - else: + sensor_data[ATTR_LOCATION] = \ + sensor_state.attributes[ATTR_SENSOR_LOCATION] + else: sensor_data[ATTR_LOCATION] = spaceapi[CONF_SPACE] # Some sensors don't have a unit of measurement if ATTR_UNIT_OF_MEASUREMENT in sensor_state.attributes: - sensor_data[ATTR_UNIT] = sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] + sensor_data[ATTR_UNIT] = \ + sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] return sensor_data @ha.callback From 423b5e16b7fe87ab25a08dad1a04a6e4f1bacf00 Mon Sep 17 00:00:00 2001 From: bouni Date: Wed, 15 May 2019 07:43:47 +0200 Subject: [PATCH 3/3] fixed minor issues pointed out by @fabaff --- homeassistant/components/spaceapi/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/spaceapi/__init__.py b/homeassistant/components/spaceapi/__init__.py index ceb757c8a98f56..975706dd06ad87 100644 --- a/homeassistant/components/spaceapi/__init__.py +++ b/homeassistant/components/spaceapi/__init__.py @@ -109,7 +109,7 @@ class APISpaceApiView(HomeAssistantView): @staticmethod def get_sensor_data(hass, spaceapi, sensor): - """get necessary data from senor""" + """Get data from a sensor.""" sensor_state = hass.states.get(sensor) if not sensor_state: return None @@ -175,7 +175,6 @@ def get(self, request): if is_sensors is not None: sensors = {} for sensor_type in is_sensors: - sensors[sensor_type] = [] sensors[sensor_type] = [] for sensor in spaceapi['sensors'][sensor_type]: sensor_data = self.get_sensor_data(hass, spaceapi, sensor)