Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select platform for La Marzocco espresso machines."""
2 
3 from collections.abc import Callable, Coroutine
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from pylamarzocco.const import MachineModel, PrebrewMode, SmartStandbyMode, SteamLevel
8 from pylamarzocco.exceptions import RequestNotSuccessful
9 from pylamarzocco.lm_machine import LaMarzoccoMachine
10 from pylamarzocco.models import LaMarzoccoMachineConfig
11 
12 from homeassistant.components.select import SelectEntity, SelectEntityDescription
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import HomeAssistantError
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN
19 from .coordinator import LaMarzoccoConfigEntry
20 from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription
21 
22 PARALLEL_UPDATES = 1
23 
24 STEAM_LEVEL_HA_TO_LM = {
25  "1": SteamLevel.LEVEL_1,
26  "2": SteamLevel.LEVEL_2,
27  "3": SteamLevel.LEVEL_3,
28 }
29 
30 STEAM_LEVEL_LM_TO_HA = {value: key for key, value in STEAM_LEVEL_HA_TO_LM.items()}
31 
32 PREBREW_MODE_HA_TO_LM = {
33  "disabled": PrebrewMode.DISABLED,
34  "prebrew": PrebrewMode.PREBREW,
35  "preinfusion": PrebrewMode.PREINFUSION,
36 }
37 
38 PREBREW_MODE_LM_TO_HA = {value: key for key, value in PREBREW_MODE_HA_TO_LM.items()}
39 
40 STANDBY_MODE_HA_TO_LM = {
41  "power_on": SmartStandbyMode.POWER_ON,
42  "last_brewing": SmartStandbyMode.LAST_BREWING,
43 }
44 
45 STANDBY_MODE_LM_TO_HA = {value: key for key, value in STANDBY_MODE_HA_TO_LM.items()}
46 
47 
48 @dataclass(frozen=True, kw_only=True)
50  LaMarzoccoEntityDescription,
51  SelectEntityDescription,
52 ):
53  """Description of a La Marzocco select entity."""
54 
55  current_option_fn: Callable[[LaMarzoccoMachineConfig], str]
56  select_option_fn: Callable[[LaMarzoccoMachine, str], Coroutine[Any, Any, bool]]
57 
58 
59 ENTITIES: tuple[LaMarzoccoSelectEntityDescription, ...] = (
61  key="steam_temp_select",
62  translation_key="steam_temp_select",
63  options=["1", "2", "3"],
64  select_option_fn=lambda machine, option: machine.set_steam_level(
65  STEAM_LEVEL_HA_TO_LM[option]
66  ),
67  current_option_fn=lambda config: STEAM_LEVEL_LM_TO_HA[config.steam_level],
68  supported_fn=lambda coordinator: coordinator.device.model
69  == MachineModel.LINEA_MICRA,
70  ),
72  key="prebrew_infusion_select",
73  translation_key="prebrew_infusion_select",
74  entity_category=EntityCategory.CONFIG,
75  options=["disabled", "prebrew", "preinfusion"],
76  select_option_fn=lambda machine, option: machine.set_prebrew_mode(
77  PREBREW_MODE_HA_TO_LM[option]
78  ),
79  current_option_fn=lambda config: PREBREW_MODE_LM_TO_HA[config.prebrew_mode],
80  supported_fn=lambda coordinator: coordinator.device.model
81  in (
82  MachineModel.GS3_AV,
83  MachineModel.LINEA_MICRA,
84  MachineModel.LINEA_MINI,
85  ),
86  ),
88  key="smart_standby_mode",
89  translation_key="smart_standby_mode",
90  entity_category=EntityCategory.CONFIG,
91  options=["power_on", "last_brewing"],
92  select_option_fn=lambda machine, option: machine.set_smart_standby(
93  enabled=machine.config.smart_standby.enabled,
94  mode=STANDBY_MODE_HA_TO_LM[option],
95  minutes=machine.config.smart_standby.minutes,
96  ),
97  current_option_fn=lambda config: STANDBY_MODE_LM_TO_HA[
98  config.smart_standby.mode
99  ],
100  ),
101 )
102 
103 
105  hass: HomeAssistant,
106  entry: LaMarzoccoConfigEntry,
107  async_add_entities: AddEntitiesCallback,
108 ) -> None:
109  """Set up select entities."""
110  coordinator = entry.runtime_data
111 
113  LaMarzoccoSelectEntity(coordinator, description)
114  for description in ENTITIES
115  if description.supported_fn(coordinator)
116  )
117 
118 
120  """La Marzocco select entity."""
121 
122  entity_description: LaMarzoccoSelectEntityDescription
123 
124  @property
125  def current_option(self) -> str:
126  """Return the current selected option."""
127  return str(
128  self.entity_descriptionentity_description.current_option_fn(self.coordinator.device.config)
129  )
130 
131  async def async_select_option(self, option: str) -> None:
132  """Change the selected option."""
133  if option != self.current_optioncurrent_optioncurrent_option:
134  try:
135  await self.entity_descriptionentity_description.select_option_fn(
136  self.coordinator.device, option
137  )
138  except RequestNotSuccessful as exc:
139  raise HomeAssistantError(
140  translation_domain=DOMAIN,
141  translation_key="select_option_error",
142  translation_placeholders={
143  "key": self.entity_descriptionentity_description.key,
144  "option": option,
145  },
146  ) from exc
147  self.async_write_ha_stateasync_write_ha_state()
None async_setup_entry(HomeAssistant hass, LaMarzoccoConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:108