1 """Support for Elgato switches."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
9 from elgato
import Elgato, ElgatoError
17 from .
import ElgatorConfigEntry
18 from .coordinator
import ElgatoData, ElgatoDataUpdateCoordinator
19 from .entity
import ElgatoEntity
22 @dataclass(frozen=True, kw_only=True)
24 """Class describing Elgato switch entities."""
26 has_fn: Callable[[ElgatoData], bool] =
lambda _:
True
27 is_on_fn: Callable[[ElgatoData], bool |
None]
28 set_fn: Callable[[Elgato, bool], Awaitable[Any]]
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),
42 translation_key=
"energy_saving",
43 entity_category=EntityCategory.CONFIG,
44 has_fn=
lambda x: x.battery
is not None,
46 x.settings.battery.energy_saving.enabled
if x.settings.battery
else None
48 set_fn=
lambda client, on: client.energy_saving(on=on),
55 entry: ElgatorConfigEntry,
56 async_add_entities: AddEntitiesCallback,
58 """Set up Elgato switches based on a config entry."""
59 coordinator = entry.runtime_data
63 coordinator=coordinator,
64 description=description,
66 for description
in SWITCHES
67 if description.has_fn(coordinator.data)
72 """Representation of an Elgato switch."""
74 entity_description: ElgatoSwitchEntityDescription
78 coordinator: ElgatoDataUpdateCoordinator,
79 description: ElgatoSwitchEntityDescription,
81 """Initiate Elgato switch."""
86 f
"{coordinator.data.info.serial_number}_{description.key}"
91 """Return state of the switch."""
95 """Turn the entity on."""
98 except ElgatoError
as error:
100 "An error occurred while updating the Elgato Light"
106 """Turn the entity off."""
108 await self.
entity_descriptionentity_description.set_fn(self.coordinator.client,
False)
109 except ElgatoError
as error:
111 "An error occurred while updating the Elgato Light"
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None __init__(self, ElgatoDataUpdateCoordinator coordinator, ElgatoSwitchEntityDescription description)
None async_setup_entry(HomeAssistant hass, ElgatorConfigEntry entry, AddEntitiesCallback async_add_entities)