Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Define switch func."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import IntellifireDataUpdateCoordinator
15 from .const import DOMAIN
16 from .entity import IntellifireEntity
17 
18 
19 @dataclass(frozen=True)
21  """Mixin for required keys."""
22 
23  on_fn: Callable[[IntellifireDataUpdateCoordinator], Awaitable]
24  off_fn: Callable[[IntellifireDataUpdateCoordinator], Awaitable]
25  value_fn: Callable[[IntellifireDataUpdateCoordinator], bool]
26 
27 
28 @dataclass(frozen=True)
30  SwitchEntityDescription, IntellifireSwitchRequiredKeysMixin
31 ):
32  """Describes a switch entity."""
33 
34 
35 INTELLIFIRE_SWITCHES: tuple[IntellifireSwitchEntityDescription, ...] = (
37  key="on_off",
38  translation_key="flame",
39  on_fn=lambda coordinator: coordinator.control_api.flame_on(),
40  off_fn=lambda coordinator: coordinator.control_api.flame_off(),
41  value_fn=lambda coordinator: coordinator.read_api.data.is_on,
42  ),
44  key="pilot",
45  translation_key="pilot_light",
46  on_fn=lambda coordinator: coordinator.control_api.pilot_on(),
47  off_fn=lambda coordinator: coordinator.control_api.pilot_off(),
48  value_fn=lambda coordinator: coordinator.read_api.data.pilot_on,
49  ),
50 )
51 
52 
54  hass: HomeAssistant,
55  entry: ConfigEntry,
56  async_add_entities: AddEntitiesCallback,
57 ) -> None:
58  """Configure switch entities."""
59  coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
60 
62  IntellifireSwitch(coordinator=coordinator, description=description)
63  for description in INTELLIFIRE_SWITCHES
64  )
65 
66 
68  """Define an Intellifire Switch."""
69 
70  entity_description: IntellifireSwitchEntityDescription
71 
72  async def async_turn_on(self, **kwargs: Any) -> None:
73  """Turn on the switch."""
74  await self.entity_descriptionentity_description.on_fn(self.coordinator)
75  await self.async_update_ha_stateasync_update_ha_state(force_refresh=True)
76 
77  async def async_turn_off(self, **kwargs: Any) -> None:
78  """Turn off the switch."""
79  await self.entity_descriptionentity_description.off_fn(self.coordinator)
80  await self.async_update_ha_stateasync_update_ha_state(force_refresh=True)
81 
82  @property
83  def is_on(self) -> bool | None:
84  """Return the on state."""
85  return self.entity_descriptionentity_description.value_fn(self.coordinator)
None async_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:942
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:57