Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Elgato switches."""
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 elgato import Elgato, ElgatoError
10 
11 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import HomeAssistantError
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import ElgatorConfigEntry
18 from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator
19 from .entity import ElgatoEntity
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Class describing Elgato switch entities."""
25 
26  has_fn: Callable[[ElgatoData], bool] = lambda _: True
27  is_on_fn: Callable[[ElgatoData], bool | None]
28  set_fn: Callable[[Elgato, bool], Awaitable[Any]]
29 
30 
31 SWITCHES = [
33  key="bypass",
34  translation_key="bypass",
35  entity_category=EntityCategory.CONFIG,
36  has_fn=lambda x: x.battery is not None,
37  is_on_fn=lambda x: x.settings.battery.bypass if x.settings.battery else None,
38  set_fn=lambda client, on: client.battery_bypass(on=on),
39  ),
41  key="energy_saving",
42  translation_key="energy_saving",
43  entity_category=EntityCategory.CONFIG,
44  has_fn=lambda x: x.battery is not None,
45  is_on_fn=lambda x: (
46  x.settings.battery.energy_saving.enabled if x.settings.battery else None
47  ),
48  set_fn=lambda client, on: client.energy_saving(on=on),
49  ),
50 ]
51 
52 
54  hass: HomeAssistant,
55  entry: ElgatorConfigEntry,
56  async_add_entities: AddEntitiesCallback,
57 ) -> None:
58  """Set up Elgato switches based on a config entry."""
59  coordinator = entry.runtime_data
60 
63  coordinator=coordinator,
64  description=description,
65  )
66  for description in SWITCHES
67  if description.has_fn(coordinator.data)
68  )
69 
70 
72  """Representation of an Elgato switch."""
73 
74  entity_description: ElgatoSwitchEntityDescription
75 
76  def __init__(
77  self,
78  coordinator: ElgatoDataUpdateCoordinator,
79  description: ElgatoSwitchEntityDescription,
80  ) -> None:
81  """Initiate Elgato switch."""
82  super().__init__(coordinator)
83 
84  self.entity_descriptionentity_description = description
85  self._attr_unique_id_attr_unique_id = (
86  f"{coordinator.data.info.serial_number}_{description.key}"
87  )
88 
89  @property
90  def is_on(self) -> bool | None:
91  """Return state of the switch."""
92  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.data)
93 
94  async def async_turn_on(self, **kwargs: Any) -> None:
95  """Turn the entity on."""
96  try:
97  await self.entity_descriptionentity_description.set_fn(self.coordinator.client, True)
98  except ElgatoError as error:
99  raise HomeAssistantError(
100  "An error occurred while updating the Elgato Light"
101  ) from error
102  finally:
103  await self.coordinator.async_refresh()
104 
105  async def async_turn_off(self, **kwargs: Any) -> None:
106  """Turn the entity off."""
107  try:
108  await self.entity_descriptionentity_description.set_fn(self.coordinator.client, False)
109  except ElgatoError as error:
110  raise HomeAssistantError(
111  "An error occurred while updating the Elgato Light"
112  ) from error
113  finally:
114  await self.coordinator.async_refresh()
None __init__(self, ElgatoDataUpdateCoordinator coordinator, ElgatoSwitchEntityDescription description)
Definition: switch.py:80
None async_setup_entry(HomeAssistant hass, ElgatorConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:57