1 """Switch platform for La Marzocco espresso machines."""
3 from collections.abc
import Callable, Coroutine
4 from dataclasses
import dataclass
7 from pylamarzocco.const
import BoilerType
8 from pylamarzocco.exceptions
import RequestNotSuccessful
9 from pylamarzocco.lm_machine
import LaMarzoccoMachine
10 from pylamarzocco.models
import LaMarzoccoMachineConfig
18 from .const
import DOMAIN
19 from .coordinator
import LaMarzoccoConfigEntry, LaMarzoccoUpdateCoordinator
20 from .entity
import LaMarzoccoBaseEntity, LaMarzoccoEntity, LaMarzoccoEntityDescription
25 @dataclass(frozen=True, kw_only=True)
27 LaMarzoccoEntityDescription,
28 SwitchEntityDescription,
30 """Description of a La Marzocco Switch."""
32 control_fn: Callable[[LaMarzoccoMachine, bool], Coroutine[Any, Any, bool]]
33 is_on_fn: Callable[[LaMarzoccoMachineConfig], bool]
36 ENTITIES: tuple[LaMarzoccoSwitchEntityDescription, ...] = (
39 translation_key=
"main",
41 control_fn=
lambda machine, state: machine.set_power(state),
42 is_on_fn=
lambda config: config.turned_on,
45 key=
"steam_boiler_enable",
46 translation_key=
"steam_boiler",
47 control_fn=
lambda machine, state: machine.set_steam(state),
48 is_on_fn=
lambda config: config.boilers[BoilerType.STEAM].enabled,
51 key=
"smart_standby_enabled",
52 translation_key=
"smart_standby_enabled",
53 entity_category=EntityCategory.CONFIG,
54 control_fn=
lambda machine, state: machine.set_smart_standby(
56 mode=machine.config.smart_standby.mode,
57 minutes=machine.config.smart_standby.minutes,
59 is_on_fn=
lambda config: config.smart_standby.enabled,
66 entry: LaMarzoccoConfigEntry,
67 async_add_entities: AddEntitiesCallback,
69 """Set up switch entities and services."""
71 coordinator = entry.runtime_data
73 entities: list[SwitchEntity] = []
76 for description
in ENTITIES
77 if description.supported_fn(coordinator)
82 for wake_up_sleep_entry_id
in coordinator.device.config.wake_up_sleep_entries
89 """Switches representing espresso machine power, prebrew, and auto on/off."""
91 entity_description: LaMarzoccoSwitchEntityDescription
96 await self.
entity_descriptionentity_description.control_fn(self.coordinator.device,
True)
97 except RequestNotSuccessful
as exc:
99 translation_domain=DOMAIN,
100 translation_key=
"switch_on_error",
106 """Turn device off."""
108 await self.
entity_descriptionentity_description.control_fn(self.coordinator.device,
False)
109 except RequestNotSuccessful
as exc:
111 translation_domain=DOMAIN,
112 translation_key=
"switch_off_error",
118 def is_on(self) -> bool:
119 """Return true if device is on."""
120 return self.
entity_descriptionentity_description.is_on_fn(self.coordinator.device.config)
124 """Switch representing espresso machine auto on/off."""
126 coordinator: LaMarzoccoUpdateCoordinator
127 _attr_translation_key =
"auto_on_off"
131 coordinator: LaMarzoccoUpdateCoordinator,
134 """Initialize the switch."""
135 super().
__init__(coordinator, f
"auto_on_off_{identifier}")
141 """Enable or disable the auto on/off schedule."""
142 wake_up_sleep_entry = self.coordinator.device.config.wake_up_sleep_entries[
145 wake_up_sleep_entry.enabled = state
147 await self.coordinator.device.set_wake_up_sleep(wake_up_sleep_entry)
148 except RequestNotSuccessful
as exc:
150 translation_domain=DOMAIN,
151 translation_key=
"auto_on_off_error",
152 translation_placeholders={
"id": self.
_identifier_identifier,
"state":
str(state)},
157 """Turn switch on."""
161 """Turn switch off."""
166 """Return true if switch is on."""
167 return self.coordinator.device.config.wake_up_sleep_entries[
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None _async_enable(self, bool state)
_attr_translation_placeholders
None __init__(self, LaMarzoccoUpdateCoordinator coordinator, str identifier)
None async_turn_on(self, **Any kwargs)
EntityCategory|None entity_category(self)
None async_write_ha_state(self)
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, LaMarzoccoConfigEntry entry, AddEntitiesCallback async_add_entities)