Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions homeassistant/components/sensor/smappee.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@
'solar_today':
['Solar Today', 'mdi:white-balance-sunny', 'remote', 'kWh', 'solar'],
'power_today':
['Power Today', 'mdi:power-plug', 'remote', 'kWh', 'consumption']
['Power Today', 'mdi:power-plug', 'remote', 'kWh', '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)
Expand All @@ -43,36 +55,50 @@ 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))
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]))
elif 'water' in SENSOR_TYPES[sensor]:
for items in smappee.info[location_id].get('sensors'):
dev.append(SmappeeSensor(
smappee,
location_id,
'{}:{}'.format(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))
else:
dev.append(SmappeeSensor(smappee, None, sensor))
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]))

add_devices(dev, True)


class SmappeeSensor(Entity):
"""Implementation of a Smappee sensor."""

def __init__(self, smappee, location_id, sensor):
"""Initialize the sensor."""
def __init__(self, smappee, location_id, sensor, attributes):
"""Initialize the Smappee 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):
Expand All @@ -82,9 +108,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):
Expand Down Expand Up @@ -160,3 +184,13 @@ 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) %s",
sensor_name, sensor_id, consumption)
value = consumption.get(self._smappe_name)
self._state = value
21 changes: 17 additions & 4 deletions homeassistant/components/smappee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)",
Expand Down