-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch.py
193 lines (147 loc) · 5.92 KB
/
switch.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
"""Support for VeSync switches."""
import logging
from homeassistant.components.switch import SwitchEntity
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, VeSyncDevice, is_humidifier
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES
_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_SWITCHES), discover)
)
_setup_entities(hass.data[DOMAIN][VS_SWITCHES], 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(VeSyncSwitchHA(dev))
elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
entities.append(VeSyncLightSwitch(dev))
elif is_humidifier(dev.device_type):
entities.append(VeSyncHumidifierDisplayHA(dev))
entities.append(VeSyncHumidifierAutomaticStopHA(dev))
entities.append(VeSyncHumidifierAutoOnHA(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 VeSyncBaseSwitch(VeSyncDevice, SwitchEntity):
"""Base class for VeSync switch Device Representations."""
def turn_on(self, **kwargs):
"""Turn the device on."""
self.device.turn_on()
class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity):
"""Representation of a VeSync switch."""
def __init__(self, plug):
"""Initialize the VeSync switch device."""
super().__init__(plug)
self.smartplug = plug
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
if not hasattr(self.smartplug, "weekly_energy_total"):
return {}
return {
"voltage": self.smartplug.voltage,
"weekly_energy_total": self.smartplug.weekly_energy_total,
"monthly_energy_total": self.smartplug.monthly_energy_total,
"yearly_energy_total": self.smartplug.yearly_energy_total,
}
def update(self):
"""Update outlet details and energy usage."""
self.smartplug.update()
self.smartplug.update_energy()
class VeSyncLightSwitch(VeSyncBaseSwitch, SwitchEntity):
"""Handle representation of VeSync Light Switch."""
def __init__(self, switch):
"""Initialize Light Switch device class."""
super().__init__(switch)
self.switch = switch
class VeSyncHumidifierSwitchEntity(VeSyncBaseEntity, SwitchEntity):
"""Representation of a switch for configuring 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 configuration entity category."""
return EntityCategory.CONFIG
class VeSyncHumidifierDisplayHA(VeSyncHumidifierSwitchEntity):
"""Representation of the display on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this display."""
return f"{super().unique_id}-display"
@property
def name(self):
"""Return the name of the display."""
return f"{super().name} display"
@property
def is_on(self):
"""Return True if display is on."""
return self.device.details["display"]
def turn_on(self, **kwargs):
"""Turn the display on."""
self.device.turn_on_display()
def turn_off(self, **kwargs):
"""Turn the display off."""
self.device.turn_off_display()
class VeSyncHumidifierAutomaticStopHA(VeSyncHumidifierSwitchEntity):
"""Representation of the automatic stop toggle on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this device."""
return f"{super().unique_id}-automatic-stop"
@property
def name(self):
"""Return the name of the device."""
return f"{super().name} automatic stop"
@property
def is_on(self):
"""Return True if automatic stop is on."""
return self.device.config["automatic_stop"]
def turn_on(self, **kwargs):
"""Turn the automatic stop on."""
self.device.automatic_stop_on()
def turn_off(self, **kwargs):
"""Turn the automatic stop off."""
self.device.automatic_stop_off()
class VeSyncHumidifierAutoOnHA(VeSyncHumidifierSwitchEntity):
"""Provide switch to turn off auto mode and set manual mist level 1 on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this device."""
return f"{super().unique_id}-auto-mode"
@property
def name(self):
"""Return the name of the device."""
return f"{super().name} auto mode"
@property
def is_on(self):
"""Return True if in auto mode."""
return self.device.details["mode"] == "auto"
def turn_on(self, **kwargs):
"""Turn auto mode on."""
self.device.set_auto_mode()
def turn_off(self, **kwargs):
"""Turn auto off by setting manual and mist level 1."""
self.device.set_manual_mode()
self.device.set_mist_level(1)