From cb359da79e6ed87ae65d3a3ebcb35d9d1677f60f Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Fri, 27 Jun 2025 10:03:14 +0200 Subject: [PATCH] Make entities unavailable when machine is physically off in lamarzocco (#147426) --- homeassistant/components/lamarzocco/entity.py | 20 ++++++++++++- homeassistant/components/lamarzocco/number.py | 4 --- homeassistant/components/lamarzocco/sensor.py | 6 ++-- tests/components/lamarzocco/test_sensor.py | 28 +++++++++++++++++-- tests/components/lamarzocco/test_switch.py | 26 +++++++++++++++-- 5 files changed, 71 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/lamarzocco/entity.py b/homeassistant/components/lamarzocco/entity.py index 6dc024645ce..6f9de083286 100644 --- a/homeassistant/components/lamarzocco/entity.py +++ b/homeassistant/components/lamarzocco/entity.py @@ -2,8 +2,10 @@ from collections.abc import Callable from dataclasses import dataclass +from typing import cast -from pylamarzocco.const import FirmwareType +from pylamarzocco.const import FirmwareType, MachineState, WidgetType +from pylamarzocco.models import MachineStatus from homeassistant.const import CONF_ADDRESS, CONF_MAC from homeassistant.helpers.device_registry import ( @@ -32,6 +34,7 @@ class LaMarzoccoBaseEntity( """Common elements for all entities.""" _attr_has_entity_name = True + _unavailable_when_machine_off = True def __init__( self, @@ -63,6 +66,21 @@ class LaMarzoccoBaseEntity( if connections: self._attr_device_info.update(DeviceInfo(connections=connections)) + @property + def available(self) -> bool: + """Return True if entity is available.""" + machine_state = ( + cast( + MachineStatus, + self.coordinator.device.dashboard.config[WidgetType.CM_MACHINE_STATUS], + ).status + if WidgetType.CM_MACHINE_STATUS in self.coordinator.device.dashboard.config + else MachineState.OFF + ) + return super().available and not ( + self._unavailable_when_machine_off and machine_state is MachineState.OFF + ) + class LaMarzoccoEntity(LaMarzoccoBaseEntity): """Common elements for all entities.""" diff --git a/homeassistant/components/lamarzocco/number.py b/homeassistant/components/lamarzocco/number.py index f8cb8b1d6fe..b235cc7c5f9 100644 --- a/homeassistant/components/lamarzocco/number.py +++ b/homeassistant/components/lamarzocco/number.py @@ -58,10 +58,6 @@ ENTITIES: tuple[LaMarzoccoNumberEntityDescription, ...] = ( CoffeeBoiler, machine.dashboard.config[WidgetType.CM_COFFEE_BOILER] ).target_temperature ), - available_fn=( - lambda coordinator: WidgetType.CM_COFFEE_BOILER - in coordinator.device.dashboard.config - ), ), LaMarzoccoNumberEntityDescription( key="smart_standby_time", diff --git a/homeassistant/components/lamarzocco/sensor.py b/homeassistant/components/lamarzocco/sensor.py index c76f51c3488..a432f5b8dae 100644 --- a/homeassistant/components/lamarzocco/sensor.py +++ b/homeassistant/components/lamarzocco/sensor.py @@ -57,10 +57,6 @@ ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = ( ).ready_start_time ), entity_category=EntityCategory.DIAGNOSTIC, - available_fn=( - lambda coordinator: WidgetType.CM_COFFEE_BOILER - in coordinator.device.dashboard.config - ), ), LaMarzoccoSensorEntityDescription( key="steam_boiler_ready_time", @@ -188,6 +184,8 @@ class LaMarzoccoSensorEntity(LaMarzoccoEntity, SensorEntity): class LaMarzoccoStatisticSensorEntity(LaMarzoccoSensorEntity): """Sensor for La Marzocco statistics.""" + _unavailable_when_machine_off = False + @property def native_value(self) -> StateType | datetime | None: """Return the value of the sensor.""" diff --git a/tests/components/lamarzocco/test_sensor.py b/tests/components/lamarzocco/test_sensor.py index 183d3f2daa6..dee2fa0b79c 100644 --- a/tests/components/lamarzocco/test_sensor.py +++ b/tests/components/lamarzocco/test_sensor.py @@ -2,11 +2,11 @@ from unittest.mock import MagicMock, patch -from pylamarzocco.const import ModelName +from pylamarzocco.const import MachineState, ModelName, WidgetType import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.const import Platform +from homeassistant.const import STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -52,3 +52,27 @@ async def test_steam_ready_entity_for_all_machines( entry = entity_registry.async_get(state.entity_id) assert entry + + +async def test_sensors_unavailable_if_machine_off( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_lamarzocco: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the La Marzocco switches are unavailable when the device is offline.""" + SWITCHES_UNAVAILABLE = ( + ("sensor.gs012345_steam_boiler_ready_time", True), + ("sensor.gs012345_coffee_boiler_ready_time", True), + ("sensor.gs012345_total_coffees_made", False), + ) + mock_lamarzocco.dashboard.config[ + WidgetType.CM_MACHINE_STATUS + ].status = MachineState.OFF + with patch("homeassistant.components.lamarzocco.PLATFORMS", [Platform.SENSOR]): + await async_init_integration(hass, mock_config_entry) + + for sensor, available in SWITCHES_UNAVAILABLE: + state = hass.states.get(sensor) + assert state + assert (state.state == STATE_UNAVAILABLE) == available diff --git a/tests/components/lamarzocco/test_switch.py b/tests/components/lamarzocco/test_switch.py index 0f1c4fd6ebb..c715c23b78f 100644 --- a/tests/components/lamarzocco/test_switch.py +++ b/tests/components/lamarzocco/test_switch.py @@ -3,7 +3,7 @@ from typing import Any from unittest.mock import MagicMock, patch -from pylamarzocco.const import SmartStandByType +from pylamarzocco.const import MachineState, SmartStandByType, WidgetType from pylamarzocco.exceptions import RequestNotSuccessful import pytest from syrupy.assertion import SnapshotAssertion @@ -13,7 +13,7 @@ from homeassistant.components.switch import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, ) -from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er @@ -197,3 +197,25 @@ async def test_switch_exceptions( blocking=True, ) assert exc_info.value.translation_key == "auto_on_off_error" + + +async def test_switches_unavailable_if_machine_off( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_lamarzocco: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the La Marzocco switches are unavailable when the device is offline.""" + mock_lamarzocco.dashboard.config[ + WidgetType.CM_MACHINE_STATUS + ].status = MachineState.OFF + with patch("homeassistant.components.lamarzocco.PLATFORMS", [Platform.SWITCH]): + await async_init_integration(hass, mock_config_entry) + + switches = er.async_entries_for_config_entry( + entity_registry, mock_config_entry.entry_id + ) + for switch in switches: + state = hass.states.get(switch.entity_id) + assert state + assert state.state == STATE_UNAVAILABLE