Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for radiotherm switches."""
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, callback
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import RadioThermUpdateCoordinator
14 from .entity import RadioThermostatEntity
15 
16 PARALLEL_UPDATES = 1
17 
18 
20  hass: HomeAssistant,
21  entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up switches for a radiotherm device."""
25  coordinator: RadioThermUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
27 
28 
30  """Provides radiotherm hold switch support."""
31 
32  _attr_translation_key = "hold"
33 
34  def __init__(self, coordinator: RadioThermUpdateCoordinator) -> None:
35  """Initialize the hold mode switch."""
36  super().__init__(coordinator)
37  self._attr_unique_id_attr_unique_id = f"{coordinator.init_data.mac}_hold"
38 
39  @callback
40  def _process_data(self) -> None:
41  """Update and validate the data from the thermostat."""
42  data = self.datadatadata.tstat
43  self._attr_is_on_attr_is_on = bool(data["hold"])
44 
45  def _set_hold(self, hold: bool) -> None:
46  """Set hold mode."""
47  self.devicedevice.hold = int(hold)
48 
49  async def _async_set_hold(self, hold: bool) -> None:
50  """Set hold mode."""
51  await self.hasshasshass.async_add_executor_job(self._set_hold_set_hold, hold)
52  self._attr_is_on_attr_is_on = hold
53  self.async_write_ha_stateasync_write_ha_state()
54  await self.coordinator.async_request_refresh()
55 
56  async def async_turn_on(self, **kwargs: Any) -> None:
57  """Enable permanent hold."""
58  await self._async_set_hold_async_set_hold(True)
59 
60  async def async_turn_off(self, **kwargs: Any) -> None:
61  """Disable permanent hold."""
62  await self._async_set_hold_async_set_hold(False)
None __init__(self, RadioThermUpdateCoordinator coordinator)
Definition: switch.py:34
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23