/
switch.py
138 lines (108 loc) · 4.29 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
"""Support for Daikin AirBase zones."""
from __future__ import annotations
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN as DAIKIN_DOMAIN, DaikinApi
ZONE_ICON = "mdi:home-circle"
STREAMER_ICON = "mdi:air-filter"
DAIKIN_ATTR_ADVANCED = "adv"
DAIKIN_ATTR_STREAMER = "streamer"
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Old way of setting up the platform.
Can only be called when a user accidentally mentions the platform in their
config. But even in that case it would have been ignored.
"""
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Daikin climate based on config_entry."""
daikin_api: DaikinApi = hass.data[DAIKIN_DOMAIN][entry.entry_id]
switches: list[DaikinZoneSwitch | DaikinStreamerSwitch] = []
if zones := daikin_api.device.zones:
switches.extend(
[
DaikinZoneSwitch(daikin_api, zone_id)
for zone_id, zone in enumerate(zones)
if zone != ("-", "0")
]
)
if daikin_api.device.support_advanced_modes:
# It isn't possible to find out from the API responses if a specific
# device supports the streamer, so assume so if it does support
# advanced modes.
switches.append(DaikinStreamerSwitch(daikin_api))
async_add_entities(switches)
class DaikinZoneSwitch(SwitchEntity):
"""Representation of a zone."""
_attr_icon = ZONE_ICON
_attr_has_entity_name = True
def __init__(self, daikin_api: DaikinApi, zone_id):
"""Initialize the zone."""
self._api = daikin_api
self._zone_id = zone_id
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self._api.device.mac}-zone{self._zone_id}"
@property
def name(self) -> str:
"""Return the name of the sensor."""
return self._api.device.zones[self._zone_id][0]
@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return self._api.device.zones[self._zone_id][1] == "1"
@property
def device_info(self) -> DeviceInfo:
"""Return a device description for device registry."""
return self._api.device_info
async def async_update(self) -> None:
"""Retrieve latest state."""
await self._api.async_update()
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the zone on."""
await self._api.device.set_zone(self._zone_id, "1")
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the zone off."""
await self._api.device.set_zone(self._zone_id, "0")
class DaikinStreamerSwitch(SwitchEntity):
"""Streamer state."""
_attr_icon = STREAMER_ICON
_attr_name = "Streamer"
_attr_has_entity_name = True
def __init__(self, daikin_api: DaikinApi) -> None:
"""Initialize streamer switch."""
self._api = daikin_api
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self._api.device.mac}-streamer"
@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return (
DAIKIN_ATTR_STREAMER in self._api.device.represent(DAIKIN_ATTR_ADVANCED)[1]
)
@property
def device_info(self) -> DeviceInfo:
"""Return a device description for device registry."""
return self._api.device_info
async def async_update(self) -> None:
"""Retrieve latest state."""
await self._api.async_update()
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the zone on."""
await self._api.device.set_streamer("on")
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the zone off."""
await self._api.device.set_streamer("off")