-
Notifications
You must be signed in to change notification settings - Fork 36
/
sensor.py
71 lines (58 loc) · 2.15 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Support for Nature Remo E energy sensor."""
import logging
from homeassistant.const import (
CONF_ACCESS_TOKEN,
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
DEVICE_CLASS_POWER,
)
from . import DOMAIN, NatureRemoBase
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Nature Remo E sensor."""
if discovery_info is None:
return
_LOGGER.debug("Setting up sensor platform.")
coordinator = hass.data[DOMAIN]["coordinator"]
appliances = coordinator.data["appliances"]
async_add_entities(
[
NatureRemoE(coordinator, appliance)
for appliance in appliances.values()
if appliance["type"] == "EL_SMART_METER"
]
)
class NatureRemoE(NatureRemoBase):
"""Implementation of a Nature Remo E sensor."""
def __init__(self, coordinator, appliance):
super().__init__(coordinator, appliance)
self._unit_of_measurement = POWER_WATT
@property
def state(self):
"""Return the state of the sensor."""
appliance = self._coordinator.data["appliances"][self._appliance_id]
smart_meter = appliance["smart_meter"]
echonetlite_properties = smart_meter["echonetlite_properties"]
measured_instantaneous = next(
value["val"] for value in echonetlite_properties if value["epc"] == 231
)
_LOGGER.debug("Current state: %sW", measured_instantaneous)
return measured_instantaneous
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_POWER
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()