Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for La Marzocco espresso machines."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from pylamarzocco.const import BoilerType, MachineModel, PhysicalKey
7 from pylamarzocco.lm_machine import LaMarzoccoMachine
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import EntityCategory, UnitOfTemperature, UnitOfTime
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .coordinator import LaMarzoccoConfigEntry
20 from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  LaMarzoccoEntityDescription, SensorEntityDescription
26 ):
27  """Description of a La Marzocco sensor."""
28 
29  value_fn: Callable[[LaMarzoccoMachine], float | int]
30 
31 
32 ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = (
34  key="drink_stats_coffee",
35  translation_key="drink_stats_coffee",
36  native_unit_of_measurement="drinks",
37  state_class=SensorStateClass.TOTAL_INCREASING,
38  value_fn=lambda device: device.statistics.drink_stats.get(PhysicalKey.A, 0),
39  available_fn=lambda device: len(device.statistics.drink_stats) > 0,
40  entity_category=EntityCategory.DIAGNOSTIC,
41  ),
43  key="drink_stats_flushing",
44  translation_key="drink_stats_flushing",
45  native_unit_of_measurement="drinks",
46  state_class=SensorStateClass.TOTAL_INCREASING,
47  value_fn=lambda device: device.statistics.total_flushes,
48  available_fn=lambda device: len(device.statistics.drink_stats) > 0,
49  entity_category=EntityCategory.DIAGNOSTIC,
50  ),
52  key="shot_timer",
53  translation_key="shot_timer",
54  native_unit_of_measurement=UnitOfTime.SECONDS,
55  state_class=SensorStateClass.MEASUREMENT,
56  device_class=SensorDeviceClass.DURATION,
57  value_fn=lambda device: device.config.brew_active_duration,
58  available_fn=lambda device: device.websocket_connected,
59  entity_category=EntityCategory.DIAGNOSTIC,
60  supported_fn=lambda coordinator: coordinator.local_connection_configured,
61  ),
63  key="current_temp_coffee",
64  translation_key="current_temp_coffee",
65  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66  suggested_display_precision=1,
67  state_class=SensorStateClass.MEASUREMENT,
68  device_class=SensorDeviceClass.TEMPERATURE,
69  value_fn=lambda device: device.config.boilers[
70  BoilerType.COFFEE
71  ].current_temperature,
72  ),
74  key="current_temp_steam",
75  translation_key="current_temp_steam",
76  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
77  suggested_display_precision=1,
78  state_class=SensorStateClass.MEASUREMENT,
79  device_class=SensorDeviceClass.TEMPERATURE,
80  value_fn=lambda device: device.config.boilers[
81  BoilerType.STEAM
82  ].current_temperature,
83  supported_fn=lambda coordinator: coordinator.device.model
84  != MachineModel.LINEA_MINI,
85  ),
86 )
87 
88 
90  hass: HomeAssistant,
91  entry: LaMarzoccoConfigEntry,
92  async_add_entities: AddEntitiesCallback,
93 ) -> None:
94  """Set up sensor entities."""
95  coordinator = entry.runtime_data
96 
98  LaMarzoccoSensorEntity(coordinator, description)
99  for description in ENTITIES
100  if description.supported_fn(coordinator)
101  )
102 
103 
105  """Sensor representing espresso machine temperature data."""
106 
107  entity_description: LaMarzoccoSensorEntityDescription
108 
109  @property
110  def native_value(self) -> int | float:
111  """State of the sensor."""
112  return self.entity_descriptionentity_description.value_fn(self.coordinator.device)
None async_setup_entry(HomeAssistant hass, LaMarzoccoConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:93