-
Notifications
You must be signed in to change notification settings - Fork 14
/
humidifier.py
180 lines (147 loc) · 5.5 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
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
"""Support for VeSync humidifiers."""
import logging
import math
from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import (
DEVICE_CLASS_HUMIDIFIER,
MODE_AUTO,
MODE_SLEEP,
SUPPORT_MODES,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .common import VeSyncDevice, HUMI_DEV_TYPE_TO_HA
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_HUMIDIFIERS
_LOGGER = logging.getLogger(__name__)
MANUAL_LOW = "manual low"
MANUAL_MID = "manual mid"
MANUAL_HIGH = "manual high"
PRESET_MODES = {
"Classic300S": [MODE_AUTO, MODE_SLEEP, MANUAL_LOW, MANUAL_MID, MANUAL_HIGH],
"Dual200S": [MODE_AUTO, MODE_SLEEP, MANUAL_LOW, MANUAL_MID, MANUAL_HIGH],
}
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the VeSync humidifier platform."""
async def async_discover(devices):
"""Add new devices to platform."""
_async_setup_entities(devices, async_add_entities)
disp = async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_HUMIDIFIERS), async_discover)
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
_async_setup_entities(hass.data[DOMAIN][VS_HUMIDIFIERS], async_add_entities)
@callback
def _async_setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
dev_list = []
for dev in devices:
if HUMI_DEV_TYPE_TO_HA.get(dev.device_type) == "humidifier":
dev_list.append(VeSyncHumidifierHA(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
continue
async_add_entities(dev_list, update_before_add=True)
class VeSyncHumidifierHA(VeSyncDevice, HumidifierEntity):
"""Representation of a VeSync humidifier."""
@property
def device_info(self):
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
"name": self.name,
"manufacturer": "Levoit",
"model": self.device.device_type,
}
@property
def is_on(self):
"""If the humidifier is currently on or off.
`self.device.device_status` is always 'on' on this device."""
return self.device.enabled
@property
def available_modes(self):
"""Return the list of available modes."""
return PRESET_MODES[self.device.device_type]
@property
def device_class(self):
"""Return the device class type."""
return DEVICE_CLASS_HUMIDIFIER
@property
def max_humidity(self):
"""Return the maximum humidity."""
return 80
@property
def min_humidity(self):
"""Return the minimum humidity."""
return 30
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_MODES
@property
def mode(self):
"""Return the current mode, e.g., sleep, auto, manual."""
mode = self.device.details["mode"]
if mode == "manual":
mist_level = self.device.details["mist_virtual_level"]
level = " low"
if mist_level < 4:
level = " low"
elif mist_level < 7:
level = " mid"
else:
level = " high"
mode += level
return mode
@property
def target_humidity(self) -> int:
"""Return the desired humidity set point."""
return self.device.config["auto_target_humidity"]
@property
def unique_info(self):
"""Return the ID of this humidifier."""
return self.device.uuid
@property
def extra_state_attributes(self):
"""Return the state attributes of the humidifier."""
attr = {}
attr["current_humidity"] = self.device.details["humidity"]
attr["mist_virtual_level"] = self.device.details["mist_virtual_level"]
attr["mist_level"] = self.device.details["mist_level"]
attr["water_lacks"] = self.device.details["water_lacks"]
attr["humidity_high"] = self.device.details["humidity_high"]
attr["water_tank_lifted"] = self.device.details["water_tank_lifted"]
attr["automatic_stop_reach_target"] = self.device.details["automatic_stop_reach_target"]
return attr
def set_mode(self, mode):
"""Set humidifier mode (auto, sleep, manual)."""
lower_mode = mode.lower()
if lower_mode not in (self.available_modes):
raise ValueError(
f"Invalid mode value: {mode} Valid values are {', '.join(self.available_modes)}."
)
if "manual" in lower_mode:
level = 3
manual_mode = lower_mode.split()[1]
if manual_mode == "low":
level = 3
elif manual_mode == "mid":
level = 6
else:
level = 9
self.device.set_mist_level(level)
else:
self.device.set_humidity_mode(lower_mode)
def set_humidity(self, humidity):
"""Set the humidity level."""
if not self.is_on:
self.device.turn_on()
self.set_mode(MODE_AUTO)
self.device.set_humidity(humidity)
def turn_off(self, **kwargs):
"""Set humidifier to off mode."""
self.device.turn_off()
def turn_on(self, **kwargs):
"""Set humidifier to on mode."""
self.device.turn_on()