From ac0050f1c16b22872b3ef2c0750a613955e907ef Mon Sep 17 00:00:00 2001 From: Henrik Nicolaisen Date: Fri, 2 Mar 2018 08:53:03 +0100 Subject: [PATCH 1/4] added support for smappee water sensors --- homeassistant/components/sensor/smappee.py | 57 ++++++++++++++++++---- homeassistant/components/smappee.py | 21 ++++++-- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/sensor/smappee.py b/homeassistant/components/sensor/smappee.py index 51595d19b1ac3..aeec85d18a13d 100644 --- a/homeassistant/components/sensor/smappee.py +++ b/homeassistant/components/sensor/smappee.py @@ -31,7 +31,20 @@ 'solar_today': ['Solar Today', 'mdi:white-balance-sunny', 'remote', 'kW', 'solar'], 'power_today': - ['Power Today', 'mdi:power-plug', 'remote', 'kW', 'consumption'] + ['Power Today', 'mdi:power-plug', 'remote', 'kW', 'consumption'], + 'water_sensor_1': + ['Water Sensor 1', 'mdi:water', 'water', 'm3', 'value1'], + 'water_sensor_2': + ['Water Sensor 2', 'mdi:water', 'water', 'm3', 'value2'], + 'water_sensor_temperature': + ['Water Sensor Temperature', 'mdi:temperature-celsius', + 'water', '°', 'temperature'], + 'water_sensor_humidity': + ['Water Sensor Humidity', 'mdi:water-percent', 'water', + '%', 'humidity'], + 'water_sensor_battery': + ['Water Sensor Battery', 'mdi:battery', 'water', + '%', 'battery'] } SCAN_INTERVAL = timedelta(seconds=30) @@ -46,33 +59,50 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for sensor in SENSOR_TYPES: if 'remote' in SENSOR_TYPES[sensor]: for location_id in smappee.locations.keys(): - dev.append(SmappeeSensor(smappee, location_id, sensor)) + dev.append(SmappeeSensor(smappee, location_id, + sensor, + SENSOR_TYPES[sensor])) + for items in smappee.info[location_id].get('sensors'): + if items.get('id'): + _LOGGER.debug("Remote sensors %s", items) + for sensor in SENSOR_TYPES: + if 'water' in SENSOR_TYPES[sensor]: + dev.append( + SmappeeSensor(smappee, + location_id, + "%s:%s" % (sensor, items.get('id')), + SENSOR_TYPES[sensor])) if smappee.is_local_active: for sensor in SENSOR_TYPES: if 'local' in SENSOR_TYPES[sensor]: if smappee.is_remote_active: for location_id in smappee.locations.keys(): - dev.append(SmappeeSensor(smappee, location_id, sensor)) + dev.append(SmappeeSensor(smappee, location_id, sensor, + SENSOR_TYPES[sensor])) else: - dev.append(SmappeeSensor(smappee, None, sensor)) + dev.append(SmappeeSensor(smappee, None, sensor, + SENSOR_TYPES[sensor])) + add_devices(dev, True) class SmappeeSensor(Entity): """Implementation of a Smappee sensor.""" - def __init__(self, smappee, location_id, sensor): + def __init__(self, smappee, location_id, sensor, attributes): """Initialize the sensor.""" self._smappee = smappee self._location_id = location_id + self._attributes = attributes self._sensor = sensor self.data = None self._state = None - self._name = SENSOR_TYPES[self._sensor][0] - self._icon = SENSOR_TYPES[self._sensor][1] - self._unit_of_measurement = SENSOR_TYPES[self._sensor][3] - self._smappe_name = SENSOR_TYPES[self._sensor][4] + self._name = self._attributes[0] + self._icon = self._attributes[1] + self._type = self._attributes[2] + self._unit_of_measurement = self._attributes[3] + self._smappe_name = self._attributes[4] @property def name(self): @@ -160,3 +190,12 @@ def update(self): if i['key'].endswith('phase5ActivePower')] power = sum(value1 + value2 + value3) / 1000 self._state = round(power, 2) + elif self._type == 'water': + sensor_name, sensor_id = self._sensor.split(":") + data = self._smappee.sensor_consumption[self._location_id]\ + .get(int(sensor_id)) + if data: + consumption = data.get('records')[-1] + _LOGGER.debug("%s %s", self._sensor, consumption) + value = consumption.get(self._smappe_name) + self._state = value diff --git a/homeassistant/components/smappee.py b/homeassistant/components/smappee.py index 0111e0437fb45..9c4f371a8f2fe 100644 --- a/homeassistant/components/smappee.py +++ b/homeassistant/components/smappee.py @@ -110,6 +110,7 @@ def __init__(self, client_id, client_secret, username, self.locations = {} self.info = {} self.consumption = {} + self.sensor_consumption = {} self.instantaneous = {} if self._remote_active or self._local_active: @@ -124,11 +125,22 @@ def update(self): for location in service_locations: location_id = location.get('serviceLocationId') if location_id is not None: + self.sensor_consumption[location_id] = {} self.locations[location_id] = location.get('name') self.info[location_id] = self._smappy \ .get_service_location_info(location_id) _LOGGER.debug("Remote info %s %s", - self.locations, self.info) + self.locations, self.info[location_id]) + + for sensors in self.info[location_id].get('sensors'): + sensor_id = sensors.get('id') + self.sensor_consumption[location_id]\ + .update({sensor_id: self.get_sensor_consumption( + location_id, sensor_id, + aggregation=3, delta=1440)}) + _LOGGER.debug("Remote sensors %s %s", + self.locations, + self.sensor_consumption[location_id]) self.consumption[location_id] = self.get_consumption( location_id, aggregation=3, delta=1440) @@ -190,7 +202,8 @@ def get_consumption(self, location_id, aggregation, delta): "Error getting comsumption from Smappee cloud. (%s)", error) - def get_sensor_consumption(self, location_id, sensor_id): + def get_sensor_consumption(self, location_id, sensor_id, + aggregation, delta): """Update data from Smappee.""" # Start & End accept epoch (in milliseconds), # datetime and pandas timestamps @@ -203,13 +216,13 @@ def get_sensor_consumption(self, location_id, sensor_id): if not self.is_remote_active: return - start = datetime.utcnow() - timedelta(minutes=30) end = datetime.utcnow() + start = end - timedelta(minutes=delta) try: return self._smappy.get_sensor_consumption(location_id, sensor_id, start, - end, 1) + end, aggregation) except RequestException as error: _LOGGER.error( "Error getting comsumption from Smappee cloud. (%s)", From a9bc572b369deee8b3d14ca066b4d2bc7a997a92 Mon Sep 17 00:00:00 2001 From: Henrik Nicolaisen Date: Fri, 2 Mar 2018 09:22:21 +0100 Subject: [PATCH 2/4] fixed lint error and wrong location_id --- homeassistant/components/sensor/smappee.py | 50 +++++++++++----------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/sensor/smappee.py b/homeassistant/components/sensor/smappee.py index aeec85d18a13d..93bb3504632c1 100644 --- a/homeassistant/components/sensor/smappee.py +++ b/homeassistant/components/sensor/smappee.py @@ -56,33 +56,34 @@ def setup_platform(hass, config, add_devices, discovery_info=None): dev = [] if smappee.is_remote_active: - for sensor in SENSOR_TYPES: - if 'remote' in SENSOR_TYPES[sensor]: - for location_id in smappee.locations.keys(): - dev.append(SmappeeSensor(smappee, location_id, - sensor, - SENSOR_TYPES[sensor])) - for items in smappee.info[location_id].get('sensors'): - if items.get('id'): - _LOGGER.debug("Remote sensors %s", items) - for sensor in SENSOR_TYPES: - if 'water' in SENSOR_TYPES[sensor]: - dev.append( - SmappeeSensor(smappee, - location_id, - "%s:%s" % (sensor, items.get('id')), - SENSOR_TYPES[sensor])) + for location_id in smappee.locations.keys(): + for sensor in SENSOR_TYPES: + if 'remote' in SENSOR_TYPES[sensor]: + dev.append(SmappeeSensor(smappee, location_id, + sensor, + SENSOR_TYPES[sensor])) + for items in smappee.info[location_id].get('sensors'): + if items.get('id'): + _LOGGER.debug("Remote sensors %s", items) + for sensor in SENSOR_TYPES: + if 'water' in SENSOR_TYPES[sensor]: + dev.append( + SmappeeSensor( + smappee, + location_id, + "%s:%s" % (sensor, items.get('id')), + SENSOR_TYPES[sensor])) if smappee.is_local_active: - for sensor in SENSOR_TYPES: - if 'local' in SENSOR_TYPES[sensor]: - if smappee.is_remote_active: - for location_id in smappee.locations.keys(): + for location_id in smappee.locations.keys(): + for sensor in SENSOR_TYPES: + if 'local' in SENSOR_TYPES[sensor]: + if smappee.is_remote_active: dev.append(SmappeeSensor(smappee, location_id, sensor, SENSOR_TYPES[sensor])) - else: - dev.append(SmappeeSensor(smappee, None, sensor, - SENSOR_TYPES[sensor])) + else: + dev.append(SmappeeSensor(smappee, None, sensor, + SENSOR_TYPES[sensor])) add_devices(dev, True) @@ -196,6 +197,7 @@ def update(self): .get(int(sensor_id)) if data: consumption = data.get('records')[-1] - _LOGGER.debug("%s %s", self._sensor, consumption) + _LOGGER.debug("%s (%s) %s", + sensor_name, sensor_id, consumption) value = consumption.get(self._smappe_name) self._state = value From 7620a4e7a5773f7ec7855d72471e9ac972e27250 Mon Sep 17 00:00:00 2001 From: Henrik Nicolaisen Date: Fri, 2 Mar 2018 09:44:56 +0100 Subject: [PATCH 3/4] fixed lint error --- homeassistant/components/sensor/smappee.py | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/sensor/smappee.py b/homeassistant/components/sensor/smappee.py index 93bb3504632c1..575cc825f242a 100644 --- a/homeassistant/components/sensor/smappee.py +++ b/homeassistant/components/sensor/smappee.py @@ -59,20 +59,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for location_id in smappee.locations.keys(): for sensor in SENSOR_TYPES: if 'remote' in SENSOR_TYPES[sensor]: - dev.append(SmappeeSensor(smappee, location_id, - sensor, - SENSOR_TYPES[sensor])) - for items in smappee.info[location_id].get('sensors'): - if items.get('id'): - _LOGGER.debug("Remote sensors %s", items) - for sensor in SENSOR_TYPES: - if 'water' in SENSOR_TYPES[sensor]: - dev.append( - SmappeeSensor( - smappee, - location_id, - "%s:%s" % (sensor, items.get('id')), - SENSOR_TYPES[sensor])) + dev.append(SmappeeSensor(smappee, location_id, + sensor, + SENSOR_TYPES[sensor])) + elif 'water' in SENSOR_TYPES[sensor]: + for items in smappee.info[location_id].get('sensors'): + dev.append(SmappeeSensor( + smappee, + location_id, + "%s:%s" % (sensor, items.get('id')), + SENSOR_TYPES[sensor])) if smappee.is_local_active: for location_id in smappee.locations.keys(): From f60a4f7830a20b81705d9f5528ccfa47002247be Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 6 Apr 2018 22:44:36 +0200 Subject: [PATCH 4/4] Use string formatting --- homeassistant/components/sensor/smappee.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/smappee.py b/homeassistant/components/sensor/smappee.py index 575cc825f242a..0c380848f281f 100644 --- a/homeassistant/components/sensor/smappee.py +++ b/homeassistant/components/sensor/smappee.py @@ -67,7 +67,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): dev.append(SmappeeSensor( smappee, location_id, - "%s:%s" % (sensor, items.get('id')), + '{}:{}'.format(sensor, items.get('id')), SENSOR_TYPES[sensor])) if smappee.is_local_active: @@ -88,7 +88,7 @@ class SmappeeSensor(Entity): """Implementation of a Smappee sensor.""" def __init__(self, smappee, location_id, sensor, attributes): - """Initialize the sensor.""" + """Initialize the Smappee sensor.""" self._smappee = smappee self._location_id = location_id self._attributes = attributes @@ -109,9 +109,7 @@ def name(self): else: location_name = 'Local' - return "{} {} {}".format(SENSOR_PREFIX, - location_name, - self._name) + return "{} {} {}".format(SENSOR_PREFIX, location_name, self._name) @property def icon(self):