-
Notifications
You must be signed in to change notification settings - Fork 1
/
humidifier.py
152 lines (120 loc) · 4.85 KB
/
humidifier.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
"""Support for VeSync humidifiers."""
import logging
from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import SUPPORT_MODES
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import VeSyncDevice, is_humidifier
from .const import DOMAIN, VS_DISCOVERY, VS_HUMIDIFIERS
_LOGGER = logging.getLogger(__name__)
MAX_HUMIDITY = 80
MIN_HUMIDITY = 30
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the VeSync humidifier platform."""
@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_HUMIDIFIERS), discover)
)
_setup_entities(hass.data[DOMAIN][VS_HUMIDIFIERS], 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(VeSyncHumidifierHA(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
continue
async_add_entities(entities, update_before_add=True)
class VeSyncHumidifierHA(VeSyncDevice, HumidifierEntity):
"""Representation of a VeSync humidifier."""
_attr_max_humidity = MAX_HUMIDITY
_attr_min_humidity = MIN_HUMIDITY
def __init__(self, humidifier):
"""Initialize the VeSync humidifier device."""
super().__init__(humidifier)
self.smarthumidifier = humidifier
@property
def available_modes(self):
"""Return the available mist modes."""
return self.device.config_dict["mist_modes"]
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_MODES
@property
def target_humidity(self):
"""Return the humidity we try to reach."""
return self.smarthumidifier.config["auto_target_humidity"]
@property
def mode(self):
"""Get the current preset mode."""
if self.smarthumidifier.details["mode"] in self.available_modes:
return self.smarthumidifier.details["mode"]
return None
@property
def is_on(self):
"""Return True if humidifier is on."""
return self.smarthumidifier.enabled # device_status is always on
@property
def unique_info(self):
"""Return the ID of this humidifier."""
return self.smarthumidifier.uuid
@property
def extra_state_attributes(self):
"""Return the state attributes of the humidifier."""
attr = {}
if "water_lacks" in self.smarthumidifier.details:
attr["water_lacks"] = self.smarthumidifier.details["water_lacks"]
if "humidity_high" in self.smarthumidifier.details:
attr["humidity_high"] = self.smarthumidifier.details["humidity_high"]
if "water_tank_lifted" in self.smarthumidifier.details:
attr["water_tank_lifted"] = self.smarthumidifier.details[
"water_tank_lifted"
]
if "automatic_stop_reach_target" in self.smarthumidifier.details:
attr["automatic_stop_reach_target"] = self.smarthumidifier.details[
"automatic_stop_reach_target"
]
if "mist_level" in self.smarthumidifier.details:
attr["mist_level"] = self.smarthumidifier.details["mist_level"]
return attr
def set_humidity(self, humidity):
"""Set the target humidity of the device."""
if humidity not in range(self.min_humidity, self.max_humidity + 1):
raise ValueError(
"{humidity} is not between {self.min_humidity} and {self.max_humidity} (inclusive)"
)
self.smarthumidifier.set_humidity(humidity)
self.schedule_update_ha_state()
def set_mode(self, mode):
"""Set the mode of the device."""
if mode not in self.available_modes:
raise ValueError(
"{mode} is not one of the valid available modes: {self.available_modes}"
)
if mode == "manual":
self.smarthumidifier.set_mist_level(
self.smarthumidifier.details["mist_level"]
)
elif mode in self.available_modes:
self.smarthumidifier.set_humidity_mode(mode)
self.schedule_update_ha_state()
def turn_on(
self,
**kwargs,
) -> None:
"""Turn the device on."""
self.smarthumidifier.turn_on()