2024-01-17 08:12:49 +00:00
|
|
|
"""Sensor platform for La Marzocco espresso machines."""
|
|
|
|
|
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2024-11-09 10:03:48 +00:00
|
|
|
from pylamarzocco.const import BoilerType, MachineModel, PhysicalKey
|
2024-12-05 09:28:57 +00:00
|
|
|
from pylamarzocco.devices.machine import LaMarzoccoMachine
|
2024-01-17 08:12:49 +00:00
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2024-12-20 11:24:15 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
PERCENTAGE,
|
|
|
|
EntityCategory,
|
|
|
|
UnitOfTemperature,
|
|
|
|
UnitOfTime,
|
|
|
|
)
|
2024-01-17 08:12:49 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2024-10-30 13:43:41 +00:00
|
|
|
from .coordinator import LaMarzoccoConfigEntry
|
2024-12-20 11:24:15 +00:00
|
|
|
from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription, LaMarzoccScaleEntity
|
2024-01-17 08:12:49 +00:00
|
|
|
|
2024-12-06 21:40:29 +00:00
|
|
|
# Coordinator is used to centralize the data updates
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2024-01-17 08:12:49 +00:00
|
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class LaMarzoccoSensorEntityDescription(
|
2024-06-10 17:59:39 +00:00
|
|
|
LaMarzoccoEntityDescription, SensorEntityDescription
|
2024-01-17 08:12:49 +00:00
|
|
|
):
|
|
|
|
"""Description of a La Marzocco sensor."""
|
|
|
|
|
2024-06-10 17:59:39 +00:00
|
|
|
value_fn: Callable[[LaMarzoccoMachine], float | int]
|
2024-01-17 08:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = (
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="shot_timer",
|
|
|
|
translation_key="shot_timer",
|
|
|
|
native_unit_of_measurement=UnitOfTime.SECONDS,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.DURATION,
|
2024-06-10 17:59:39 +00:00
|
|
|
value_fn=lambda device: device.config.brew_active_duration,
|
|
|
|
available_fn=lambda device: device.websocket_connected,
|
2024-01-17 08:12:49 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2024-01-17 10:07:43 +00:00
|
|
|
supported_fn=lambda coordinator: coordinator.local_connection_configured,
|
2024-01-17 08:12:49 +00:00
|
|
|
),
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="current_temp_coffee",
|
|
|
|
translation_key="current_temp_coffee",
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
2024-02-04 10:29:32 +00:00
|
|
|
suggested_display_precision=1,
|
2024-01-17 08:12:49 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2024-06-10 17:59:39 +00:00
|
|
|
value_fn=lambda device: device.config.boilers[
|
|
|
|
BoilerType.COFFEE
|
|
|
|
].current_temperature,
|
2024-01-17 08:12:49 +00:00
|
|
|
),
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="current_temp_steam",
|
|
|
|
translation_key="current_temp_steam",
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
2024-02-04 10:29:32 +00:00
|
|
|
suggested_display_precision=1,
|
2024-01-17 08:12:49 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2024-06-10 17:59:39 +00:00
|
|
|
value_fn=lambda device: device.config.boilers[
|
|
|
|
BoilerType.STEAM
|
|
|
|
].current_temperature,
|
2024-06-12 21:35:51 +00:00
|
|
|
supported_fn=lambda coordinator: coordinator.device.model
|
|
|
|
!= MachineModel.LINEA_MINI,
|
2024-01-17 08:12:49 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-12-15 20:31:18 +00:00
|
|
|
STATISTIC_ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = (
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="drink_stats_coffee",
|
|
|
|
translation_key="drink_stats_coffee",
|
|
|
|
native_unit_of_measurement="drinks",
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
value_fn=lambda device: device.statistics.drink_stats.get(PhysicalKey.A, 0),
|
|
|
|
available_fn=lambda device: len(device.statistics.drink_stats) > 0,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="drink_stats_flushing",
|
|
|
|
translation_key="drink_stats_flushing",
|
|
|
|
native_unit_of_measurement="drinks",
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
value_fn=lambda device: device.statistics.total_flushes,
|
|
|
|
available_fn=lambda device: len(device.statistics.drink_stats) > 0,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-12-20 11:24:15 +00:00
|
|
|
SCALE_ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = (
|
|
|
|
LaMarzoccoSensorEntityDescription(
|
|
|
|
key="scale_battery",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
value_fn=lambda device: (
|
|
|
|
device.config.scale.battery if device.config.scale else 0
|
|
|
|
),
|
|
|
|
supported_fn=(
|
|
|
|
lambda coordinator: coordinator.device.model == MachineModel.LINEA_MINI
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-01-17 08:12:49 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2024-06-11 20:27:47 +00:00
|
|
|
entry: LaMarzoccoConfigEntry,
|
2024-01-17 08:12:49 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up sensor entities."""
|
2024-12-15 20:31:18 +00:00
|
|
|
config_coordinator = entry.runtime_data.config_coordinator
|
2024-01-17 08:12:49 +00:00
|
|
|
|
2024-12-15 20:31:18 +00:00
|
|
|
entities = [
|
|
|
|
LaMarzoccoSensorEntity(config_coordinator, description)
|
2024-01-17 10:07:43 +00:00
|
|
|
for description in ENTITIES
|
2024-12-15 20:31:18 +00:00
|
|
|
if description.supported_fn(config_coordinator)
|
|
|
|
]
|
|
|
|
|
2024-12-20 11:24:15 +00:00
|
|
|
if (
|
|
|
|
config_coordinator.device.model == MachineModel.LINEA_MINI
|
|
|
|
and config_coordinator.device.config.scale
|
|
|
|
):
|
|
|
|
entities.extend(
|
|
|
|
LaMarzoccoScaleSensorEntity(config_coordinator, description)
|
|
|
|
for description in SCALE_ENTITIES
|
|
|
|
)
|
|
|
|
|
2024-12-15 20:31:18 +00:00
|
|
|
statistics_coordinator = entry.runtime_data.statistics_coordinator
|
|
|
|
entities.extend(
|
|
|
|
LaMarzoccoSensorEntity(statistics_coordinator, description)
|
|
|
|
for description in STATISTIC_ENTITIES
|
|
|
|
if description.supported_fn(statistics_coordinator)
|
2024-01-17 10:07:43 +00:00
|
|
|
)
|
2024-01-17 08:12:49 +00:00
|
|
|
|
2024-12-20 11:24:15 +00:00
|
|
|
def _async_add_new_scale() -> None:
|
|
|
|
async_add_entities(
|
|
|
|
LaMarzoccoScaleSensorEntity(config_coordinator, description)
|
|
|
|
for description in SCALE_ENTITIES
|
|
|
|
)
|
|
|
|
|
|
|
|
config_coordinator.new_device_callback.append(_async_add_new_scale)
|
|
|
|
|
2024-12-15 20:31:18 +00:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
2024-01-17 08:12:49 +00:00
|
|
|
|
|
|
|
class LaMarzoccoSensorEntity(LaMarzoccoEntity, SensorEntity):
|
|
|
|
"""Sensor representing espresso machine temperature data."""
|
|
|
|
|
|
|
|
entity_description: LaMarzoccoSensorEntityDescription
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> int | float:
|
|
|
|
"""State of the sensor."""
|
2024-06-10 17:59:39 +00:00
|
|
|
return self.entity_description.value_fn(self.coordinator.device)
|
2024-12-20 11:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LaMarzoccoScaleSensorEntity(LaMarzoccoSensorEntity, LaMarzoccScaleEntity):
|
|
|
|
"""Sensor for a La Marzocco scale."""
|
|
|
|
|
|
|
|
entity_description: LaMarzoccoSensorEntityDescription
|