forked from yutoyazaki/hass-nature-remo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sensor.py
172 lines (138 loc) · 5.25 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""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,
TEMP_CELSIUS,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE,
DEVICE_CLASS_HUMIDITY,
LIGHT_LUX,
DEVICE_CLASS_ILLUMINANCE,
)
from . import DOMAIN, NatureRemoBase, NatureRemoDeviceBase
_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"]
devices = coordinator.data["devices"]
entities = [
NatureRemoE(coordinator, appliance)
for appliance in appliances.values()
if appliance["type"] == "EL_SMART_METER"
]
for device in devices.values():
for sensor in device["newest_events"].keys():
if sensor == "te":
entities.append(NatureRemoTemperatureSensor(coordinator, device))
elif sensor == "hu":
entities.append(NatureRemoHumiditySensor(coordinator, device))
elif sensor == "il":
entities.append(NatureRemoIlluminanceSensor(coordinator, device))
async_add_entities(entities)
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()
class NatureRemoTemperatureSensor(NatureRemoDeviceBase):
"""Implementation of a Nature Remo sensor."""
def __init__(self, coordinator, appliance):
super().__init__(coordinator, appliance)
self._name = self._name.strip() + " Temperature"
@property
def unique_id(self):
"""Return a unique ID."""
return self._device["id"] + "-te"
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return TEMP_CELSIUS
@property
def state(self):
"""Return the state of the sensor."""
device = self._coordinator.data["devices"][self._device["id"]]
return device["newest_events"]["te"]["val"]
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TEMPERATURE
class NatureRemoHumiditySensor(NatureRemoDeviceBase):
"""Implementation of a Nature Remo sensor."""
def __init__(self, coordinator, appliance):
super().__init__(coordinator, appliance)
self._name = self._name.strip() + " Humidity"
@property
def unique_id(self):
"""Return a unique ID."""
return self._device["id"] + "-hu"
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return PERCENTAGE
@property
def state(self):
"""Return the state of the sensor."""
device = self._coordinator.data["devices"][self._device["id"]]
return device["newest_events"]["hu"]["val"]
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_HUMIDITY
class NatureRemoIlluminanceSensor(NatureRemoDeviceBase):
"""Implementation of a Nature Remo sensor."""
def __init__(self, coordinator, appliance):
super().__init__(coordinator, appliance)
self._name = self._name.strip() + " Illuminance"
@property
def unique_id(self):
"""Return a unique ID."""
return self._device["id"] + "-il"
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return LIGHT_LUX
@property
def state(self):
"""Return the state of the sensor."""
device = self._coordinator.data["devices"][self._device["id"]]
return device["newest_events"]["il"]["val"]
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_ILLUMINANCE