Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Daikin AirBase zones."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import DOMAIN
13 from .coordinator import DaikinCoordinator
14 from .entity import DaikinEntity
15 
16 DAIKIN_ATTR_ADVANCED = "adv"
17 DAIKIN_ATTR_STREAMER = "streamer"
18 DAIKIN_ATTR_MODE = "mode"
19 
20 
22  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
23 ) -> None:
24  """Set up Daikin climate based on config_entry."""
25  daikin_api: DaikinCoordinator = hass.data[DOMAIN][entry.entry_id]
26  switches: list[SwitchEntity] = []
27  if zones := daikin_api.device.zones:
28  switches.extend(
29  DaikinZoneSwitch(daikin_api, zone_id)
30  for zone_id, zone in enumerate(zones)
31  if zone[0] != "-"
32  )
33  if daikin_api.device.support_advanced_modes:
34  # It isn't possible to find out from the API responses if a specific
35  # device supports the streamer, so assume so if it does support
36  # advanced modes.
37  switches.append(DaikinStreamerSwitch(daikin_api))
38  switches.append(DaikinToggleSwitch(daikin_api))
39  async_add_entities(switches)
40 
41 
43  """Representation of a zone."""
44 
45  _attr_translation_key = "zone"
46 
47  def __init__(self, coordinator: DaikinCoordinator, zone_id: int) -> None:
48  """Initialize the zone."""
49  super().__init__(coordinator)
50  self._zone_id_zone_id = zone_id
51  self._attr_unique_id_attr_unique_id = f"{self.device.mac}-zone{zone_id}"
52 
53  @property
54  def name(self) -> str:
55  """Return the name of the sensor."""
56  return self.devicedevicedevice.zones[self._zone_id_zone_id][0]
57 
58  @property
59  def is_on(self) -> bool:
60  """Return the state of the sensor."""
61  return self.devicedevicedevice.zones[self._zone_id_zone_id][1] == "1"
62 
63  async def async_turn_on(self, **kwargs: Any) -> None:
64  """Turn the zone on."""
65  await self.devicedevicedevice.set_zone(self._zone_id_zone_id, "zone_onoff", "1")
66  await self.coordinator.async_refresh()
67 
68  async def async_turn_off(self, **kwargs: Any) -> None:
69  """Turn the zone off."""
70  await self.devicedevicedevice.set_zone(self._zone_id_zone_id, "zone_onoff", "0")
71  await self.coordinator.async_refresh()
72 
73 
75  """Streamer state."""
76 
77  _attr_name = "Streamer"
78  _attr_translation_key = "streamer"
79 
80  def __init__(self, coordinator: DaikinCoordinator) -> None:
81  """Initialize switch."""
82  super().__init__(coordinator)
83  self._attr_unique_id_attr_unique_id = f"{self.device.mac}-streamer"
84 
85  @property
86  def is_on(self) -> bool:
87  """Return the state of the sensor."""
88  return DAIKIN_ATTR_STREAMER in self.devicedevicedevice.represent(DAIKIN_ATTR_ADVANCED)[1]
89 
90  async def async_turn_on(self, **kwargs: Any) -> None:
91  """Turn the zone on."""
92  await self.devicedevicedevice.set_streamer("on")
93  await self.coordinator.async_refresh()
94 
95  async def async_turn_off(self, **kwargs: Any) -> None:
96  """Turn the zone off."""
97  await self.devicedevicedevice.set_streamer("off")
98  await self.coordinator.async_refresh()
99 
100 
102  """Switch state."""
103 
104  _attr_translation_key = "toggle"
105 
106  def __init__(self, coordinator: DaikinCoordinator) -> None:
107  """Initialize switch."""
108  super().__init__(coordinator)
109  self._attr_unique_id_attr_unique_id = f"{self.device.mac}-toggle"
110 
111  @property
112  def is_on(self) -> bool:
113  """Return the state of the sensor."""
114  return "off" not in self.devicedevicedevice.represent(DAIKIN_ATTR_MODE)
115 
116  async def async_turn_on(self, **kwargs: Any) -> None:
117  """Turn the zone on."""
118  await self.devicedevicedevice.set({})
119  await self.coordinator.async_refresh()
120 
121  async def async_turn_off(self, **kwargs: Any) -> None:
122  """Turn the zone off."""
123  await self.devicedevicedevice.set({DAIKIN_ATTR_MODE: "off"})
124  await self.coordinator.async_refresh()
None __init__(self, DaikinCoordinator coordinator)
Definition: switch.py:80
None __init__(self, DaikinCoordinator coordinator)
Definition: switch.py:106
None __init__(self, DaikinCoordinator coordinator, int zone_id)
Definition: switch.py:47
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23