Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Nexia switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from nexia.const import OPERATION_MODE_OFF
8 from nexia.thermostat import NexiaThermostat
9 from nexia.zone import NexiaThermostatZone
10 
11 from homeassistant.components.switch import SwitchEntity
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .coordinator import NexiaDataUpdateCoordinator
16 from .entity import NexiaThermostatEntity, NexiaThermostatZoneEntity
17 from .types import NexiaConfigEntry
18 
19 
21  hass: HomeAssistant,
22  config_entry: NexiaConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up switches for a Nexia device."""
26  coordinator = config_entry.runtime_data
27  nexia_home = coordinator.nexia_home
28  entities: list[NexiaHoldSwitch | NexiaEmergencyHeatSwitch] = []
29  for thermostat_id in nexia_home.get_thermostat_ids():
30  thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id)
31  if thermostat.has_emergency_heat():
32  entities.append(NexiaEmergencyHeatSwitch(coordinator, thermostat))
33  for zone_id in thermostat.get_zone_ids():
34  zone: NexiaThermostatZone = thermostat.get_zone_by_id(zone_id)
35  entities.append(NexiaHoldSwitch(coordinator, zone))
36 
37  async_add_entities(entities)
38 
39 
41  """Provides Nexia hold switch support."""
42 
43  _attr_translation_key = "hold"
44 
45  def __init__(
46  self, coordinator: NexiaDataUpdateCoordinator, zone: NexiaThermostatZone
47  ) -> None:
48  """Initialize the hold mode switch."""
49  zone_id = zone.zone_id
50  super().__init__(coordinator, zone, zone_id)
51 
52  @property
53  def is_on(self) -> bool:
54  """Return if the zone is in hold mode."""
55  return self._zone_zone.is_in_permanent_hold()
56 
57  async def async_turn_on(self, **kwargs: Any) -> None:
58  """Enable permanent hold."""
59  if self._zone_zone.get_current_mode() == OPERATION_MODE_OFF:
60  await self._zone_zone.call_permanent_off()
61  else:
62  await self._zone_zone.set_permanent_hold()
63  self._signal_zone_update_signal_zone_update()
64 
65  async def async_turn_off(self, **kwargs: Any) -> None:
66  """Disable permanent hold."""
67  await self._zone_zone.call_return_to_schedule()
68  self._signal_zone_update_signal_zone_update()
69 
70 
72  """Provides Nexia emergency heat switch support."""
73 
74  _attr_translation_key = "emergency_heat"
75 
76  def __init__(
77  self, coordinator: NexiaDataUpdateCoordinator, thermostat: NexiaThermostat
78  ) -> None:
79  """Initialize the emergency heat mode switch."""
80  super().__init__(
81  coordinator,
82  thermostat,
83  unique_id=f"{thermostat.thermostat_id}_emergency_heat",
84  )
85 
86  @property
87  def is_on(self) -> bool:
88  """Return if the zone is in hold mode."""
89  return self._thermostat_thermostat.is_emergency_heat_active()
90 
91  async def async_turn_on(self, **kwargs: Any) -> None:
92  """Enable permanent hold."""
93  await self._thermostat_thermostat.set_emergency_heat(True)
94  self._signal_thermostat_update_signal_thermostat_update()
95 
96  async def async_turn_off(self, **kwargs: Any) -> None:
97  """Disable permanent hold."""
98  await self._thermostat_thermostat.set_emergency_heat(False)
99  self._signal_thermostat_update_signal_thermostat_update()
None __init__(self, NexiaDataUpdateCoordinator coordinator, NexiaThermostat thermostat)
Definition: switch.py:78
None __init__(self, NexiaDataUpdateCoordinator coordinator, NexiaThermostatZone zone)
Definition: switch.py:47
None async_setup_entry(HomeAssistant hass, NexiaConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:24