-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor.py
201 lines (155 loc) · 5.85 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Support for power & energy sensors for VeSync outlets."""
import logging
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENERGY_KILO_WATT_HOUR, PERCENTAGE, POWER_WATT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import VeSyncBaseEntity, is_humidifier
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SENSORS
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up switches."""
@callback
def discover(devices):
"""Add new devices to platform."""
_setup_entities(devices, async_add_entities)
config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SENSORS), discover)
)
_setup_entities(hass.data[DOMAIN][VS_SENSORS], async_add_entities)
@callback
def _setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
entities = []
for dev in devices:
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
entities.append(VeSyncPowerSensor(dev))
entities.append(VeSyncEnergySensor(dev))
elif is_humidifier(dev.device_type):
entities.append(VeSyncHumiditySensor(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
async_add_entities(entities, update_before_add=True)
class VeSyncOutletSensorEntity(VeSyncBaseEntity, SensorEntity):
"""Representation of a sensor describing diagnostics of a VeSync outlet."""
def __init__(self, plug):
"""Initialize the VeSync outlet device."""
super().__init__(plug)
self.smartplug = plug
@property
def entity_category(self):
"""Return the diagnostic entity category."""
return EntityCategory.DIAGNOSTIC
class VeSyncPowerSensor(VeSyncOutletSensorEntity):
"""Representation of current power use for a VeSync outlet."""
@property
def unique_id(self):
"""Return unique ID for power sensor on device."""
return f"{super().unique_id}-power"
@property
def name(self):
"""Return sensor name."""
return f"{super().name} current power"
@property
def device_class(self):
"""Return the power device class."""
return SensorDeviceClass.POWER
@property
def native_value(self):
"""Return the current power usage in W."""
return self.smartplug.power
@property
def native_unit_of_measurement(self):
"""Return the Watt unit of measurement."""
return POWER_WATT
@property
def state_class(self):
"""Return the measurement state class."""
return SensorStateClass.MEASUREMENT
def update(self):
"""Update outlet details and energy usage."""
self.smartplug.update()
self.smartplug.update_energy()
class VeSyncEnergySensor(VeSyncOutletSensorEntity):
"""Representation of current day's energy use for a VeSync outlet."""
def __init__(self, plug):
"""Initialize the VeSync outlet device."""
super().__init__(plug)
self.smartplug = plug
@property
def unique_id(self):
"""Return unique ID for power sensor on device."""
return f"{super().unique_id}-energy"
@property
def name(self):
"""Return sensor name."""
return f"{super().name} energy use today"
@property
def device_class(self):
"""Return the energy device class."""
return SensorDeviceClass.ENERGY
@property
def native_value(self):
"""Return the today total energy usage in kWh."""
return self.smartplug.energy_today
@property
def native_unit_of_measurement(self):
"""Return the kWh unit of measurement."""
return ENERGY_KILO_WATT_HOUR
@property
def state_class(self):
"""Return the total_increasing state class."""
return SensorStateClass.TOTAL_INCREASING
def update(self):
"""Update outlet details and energy usage."""
self.smartplug.update()
self.smartplug.update_energy()
class VeSyncHumidifierSensorEntity(VeSyncBaseEntity, SensorEntity):
"""Representation of a sensor describing diagnostics of a VeSync humidifier."""
def __init__(self, humidifier):
"""Initialize the VeSync humidifier device."""
super().__init__(humidifier)
self.smarthumidifier = humidifier
@property
def entity_category(self):
"""Return the diagnostic entity category."""
return EntityCategory.DIAGNOSTIC
class VeSyncHumiditySensor(VeSyncHumidifierSensorEntity):
"""Representation of current humidity for a VeSync humidifier."""
@property
def unique_id(self):
"""Return unique ID for humidity sensor on device."""
return f"{super().unique_id}-humidity"
@property
def name(self):
"""Return sensor name."""
return f"{super().name} current humidity"
@property
def device_class(self):
"""Return the humidity device class."""
return SensorDeviceClass.HUMIDITY
@property
def native_value(self):
"""Return the current humidity in percent."""
return self.smarthumidifier.details["humidity"]
@property
def native_unit_of_measurement(self):
"""Return the % unit of measurement."""
return PERCENTAGE
@property
def state_class(self):
"""Return the measurement state class."""
return SensorStateClass.MEASUREMENT