Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Honeywell switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiosomecomfort import SomeComfortError
8 from aiosomecomfort.device import Device as SomeComfortDevice
9 
11  SwitchDeviceClass,
12  SwitchEntity,
13  SwitchEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 from homeassistant.helpers.device_registry import DeviceInfo
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import HoneywellData
22 from .const import DOMAIN
23 
24 EMERGENCY_HEAT_KEY = "emergency_heat"
25 
26 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
28  key=EMERGENCY_HEAT_KEY,
29  translation_key=EMERGENCY_HEAT_KEY,
30  device_class=SwitchDeviceClass.SWITCH,
31  ),
32 )
33 
34 
36  hass: HomeAssistant,
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up the Honeywell switches."""
41  data: HoneywellData = hass.data[DOMAIN][config_entry.entry_id]
43  HoneywellSwitch(data, device, description)
44  for device in data.devices.values()
45  if device.raw_ui_data.get("SwitchEmergencyHeatAllowed")
46  for description in SWITCH_TYPES
47  )
48 
49 
51  """Representation of a honeywell switch."""
52 
53  _attr_has_entity_name = True
54 
55  def __init__(
56  self,
57  honeywell_data: HoneywellData,
58  device: SomeComfortDevice,
59  description: SwitchEntityDescription,
60  ) -> None:
61  """Initialize the switch."""
62  self._data_data = honeywell_data
63  self._device_device = device
64  self.entity_descriptionentity_description = description
65  self._attr_unique_id_attr_unique_id = f"{device.deviceid}_{description.key}"
66  self._attr_device_info_attr_device_info = DeviceInfo(
67  identifiers={(DOMAIN, device.deviceid)},
68  name=device.name,
69  manufacturer="Honeywell",
70  )
71 
72  async def async_turn_on(self, **kwargs: Any) -> None:
73  """Turn the switch on if heat mode is enabled."""
74  try:
75  await self._device_device.set_system_mode("emheat")
76  except SomeComfortError as err:
77  raise HomeAssistantError(
78  translation_domain=DOMAIN, translation_key="switch_failed_on"
79  ) from err
80 
81  async def async_turn_off(self, **kwargs: Any) -> None:
82  """Turn the switch off if on."""
83  if self.is_onis_onis_on:
84  try:
85  await self._device_device.set_system_mode("off")
86 
87  except SomeComfortError as err:
88  raise HomeAssistantError(
89  translation_domain=DOMAIN, translation_key="switch_failed_off"
90  ) from err
91 
92  @property
93  def is_on(self) -> bool:
94  """Return true if Emergency heat is enabled."""
95  return self._device_device.system_mode == "emheat"
None __init__(self, HoneywellData honeywell_data, SomeComfortDevice device, SwitchEntityDescription description)
Definition: switch.py:60
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:39