Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch platform for Advantage Air integration."""
2 
3 from typing import Any
4 
5 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
6 from homeassistant.core import HomeAssistant
7 from homeassistant.helpers.entity_platform import AddEntitiesCallback
8 
9 from . import AdvantageAirDataConfigEntry
10 from .const import (
11  ADVANTAGE_AIR_AUTOFAN_ENABLED,
12  ADVANTAGE_AIR_STATE_OFF,
13  ADVANTAGE_AIR_STATE_ON,
14 )
15 from .entity import AdvantageAirAcEntity, AdvantageAirThingEntity
16 from .models import AdvantageAirData
17 
18 
20  hass: HomeAssistant,
21  config_entry: AdvantageAirDataConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up AdvantageAir switch platform."""
25 
26  instance = config_entry.runtime_data
27 
28  entities: list[SwitchEntity] = []
29  if aircons := instance.coordinator.data.get("aircons"):
30  for ac_key, ac_device in aircons.items():
31  if ac_device["info"]["freshAirStatus"] != "none":
32  entities.append(AdvantageAirFreshAir(instance, ac_key))
33  if ADVANTAGE_AIR_AUTOFAN_ENABLED in ac_device["info"]:
34  entities.append(AdvantageAirMyFan(instance, ac_key))
35  if things := instance.coordinator.data.get("myThings"):
36  entities.extend(
37  AdvantageAirRelay(instance, thing)
38  for thing in things["things"].values()
39  if thing["channelDipState"] == 8 # 8 = Other relay
40  )
41  async_add_entities(entities)
42 
43 
45  """Representation of Advantage Air fresh air control."""
46 
47  _attr_icon = "mdi:air-filter"
48  _attr_name = "Fresh air"
49  _attr_device_class = SwitchDeviceClass.SWITCH
50 
51  def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
52  """Initialize an Advantage Air fresh air control."""
53  super().__init__(instance, ac_key)
54  self._attr_unique_id += "-freshair"
55 
56  @property
57  def is_on(self) -> bool:
58  """Return the fresh air status."""
59  return self._ac_ac["freshAirStatus"] == ADVANTAGE_AIR_STATE_ON
60 
61  async def async_turn_on(self, **kwargs: Any) -> None:
62  """Turn fresh air on."""
63  await self.async_update_acasync_update_ac({"freshAirStatus": ADVANTAGE_AIR_STATE_ON})
64 
65  async def async_turn_off(self, **kwargs: Any) -> None:
66  """Turn fresh air off."""
67  await self.async_update_acasync_update_ac({"freshAirStatus": ADVANTAGE_AIR_STATE_OFF})
68 
69 
71  """Representation of Advantage Air MyFan control."""
72 
73  _attr_icon = "mdi:fan-auto"
74  _attr_name = "MyFan"
75  _attr_device_class = SwitchDeviceClass.SWITCH
76 
77  def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
78  """Initialize an Advantage Air MyFan control."""
79  super().__init__(instance, ac_key)
80  self._attr_unique_id += "-myfan"
81 
82  @property
83  def is_on(self) -> bool:
84  """Return the MyFan status."""
85  return self._ac_ac[ADVANTAGE_AIR_AUTOFAN_ENABLED]
86 
87  async def async_turn_on(self, **kwargs: Any) -> None:
88  """Turn MyFan on."""
89  await self.async_update_acasync_update_ac({ADVANTAGE_AIR_AUTOFAN_ENABLED: True})
90 
91  async def async_turn_off(self, **kwargs: Any) -> None:
92  """Turn MyFan off."""
93  await self.async_update_acasync_update_ac({ADVANTAGE_AIR_AUTOFAN_ENABLED: False})
94 
95 
97  """Representation of Advantage Air Thing."""
98 
99  _attr_device_class = SwitchDeviceClass.SWITCH
None __init__(self, AdvantageAirData instance, str ac_key)
Definition: switch.py:51
None __init__(self, AdvantageAirData instance, str ac_key)
Definition: switch.py:77
None async_setup_entry(HomeAssistant hass, AdvantageAirDataConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23