forked from elupus/hass_nibe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.py
53 lines (39 loc) · 1.59 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
"""Nibe Switch."""
from __future__ import annotations
import logging
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from . import NibeData, NibeSystem
from .const import CONF_SWITCHES, DATA_NIBE_ENTRIES
from .entity import NibeParameterEntity
PARALLEL_UPDATES = 0
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
):
"""Set up the device based on a config entry."""
data: NibeData = hass.data[DATA_NIBE_ENTRIES][entry.entry_id]
entities = []
for system in data.systems.values():
for parameter_id in system.config[CONF_SWITCHES]:
entities.append(NibeSwitch(system, parameter_id))
async_add_entities(entities, True)
class NibeSwitch(NibeParameterEntity, SwitchEntity):
"""Nibe Switch Entity."""
def __init__(self, system: NibeSystem, parameter_id):
"""Init."""
super().__init__(system, parameter_id, ENTITY_ID_FORMAT)
@property
def is_on(self):
"""Return if entity is on."""
if (data := self.get_raw(self._parameter_id)) is not None:
return data == "1"
else:
return None
async def async_turn_on(self, **kwargs):
"""Turn entity on."""
await self._uplink.put_parameter(self._system_id, self._parameter_id, "1")
async def async_turn_off(self, **kwargs):
"""Turn entity off."""
await self._uplink.put_parameter(self._system_id, self._parameter_id, "0")