-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_sensor.py
99 lines (74 loc) · 3.14 KB
/
binary_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
"""Support for power & energy sensors for VeSync outlets."""
import logging
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
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_BINARY_SENSORS
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up binary sensors."""
@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_BINARY_SENSORS), discover)
)
_setup_entities(hass.data[DOMAIN][VS_BINARY_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 is_humidifier(dev.device_type):
entities.append(VeSyncOutOfWaterSensor(dev))
entities.append(VeSyncWaterTankLiftedSensor(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
async_add_entities(entities, update_before_add=True)
class VeSyncHumidifierBinarySensorEntity(VeSyncBaseEntity, BinarySensorEntity):
"""Representation of a binary 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 VeSyncOutOfWaterSensor(VeSyncHumidifierBinarySensorEntity):
@property
def unique_id(self):
"""Return unique ID for out of water sensor on device."""
return f"{super().unique_id}-out_of_water"
@property
def name(self):
"""Return sensor name."""
return f"{super().name} out of water"
@property
def is_on(self) -> bool:
"""Return a value indicating whether the Humidifier is out of water."""
return self.smarthumidifier.details["water_lacks"]
class VeSyncWaterTankLiftedSensor(VeSyncHumidifierBinarySensorEntity):
@property
def unique_id(self):
"""Return unique ID for water tank lifted sensor on device."""
return f"{super().unique_id}-water_tank_lifted"
@property
def name(self):
"""Return sensor name."""
return f"{super().name} water tank lifted"
@property
def is_on(self) -> bool:
"""Return a value indicating whether the Humidifier's water tank is lifted."""
return self.smarthumidifier.details["water_tank_lifted"]