-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
63 lines (52 loc) · 1.89 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
# switch.py
import time
from homeassistant.components.switch import SwitchEntity
from . import DOMAIN
import logging
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Voltalis switch from a config entry."""
_LOGGER.debug("Setup voltalis switches")
voltalis = hass.data[DOMAIN][config_entry.entry_id]
switches = [VoltalisSwitch(voltalis, device) for device in voltalis.get_modulators()]
async_add_entities(switches)
class VoltalisSwitch(SwitchEntity):
def __init__(self, voltalis, device):
self.last_update = 0
self._voltalis = voltalis
self._state = None
self._device_id = device["csLinkId"]
self._name = device["name"]
@property
def icon(self):
if self.is_on:
return "mdi:radiator"
else:
return "mdi:radiator-off"
@property
def unique_id(self):
return f"voltalis_{self._device_id}"
@property
def name(self):
return f"Voltalis {self._name}[{self._device_id}] switch"
@property
def is_on(self):
return self._state
async def async_turn_on(self, **kwargs):
await self._voltalis.switch_turn_on_off_device(self._device_id, True)
self._state = True
async def async_turn_off(self, **kwargs):
await self._voltalis.switch_turn_on_off_device(self._device_id, False)
self._state = False
async def async_update(self):
# Retourne les datas sauvegardé si nouveau call < 10 min
if self.last_update + 60 * 10 <= time.time():
self._state = await self._voltalis.fetch_switch_device_status(self._device_id)
self.last_update = time.time()
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._device_id)},
"name": self._name,
"manufacturer": "Voltalis",
}