From 4cc538b5ae1442e35fdc5301ce49358ac1aad478 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Wed, 7 May 2025 21:48:52 +0200 Subject: [PATCH] Add sensor for brew start time to lamarzocco (#144423) * Add sensor for brew start time to lamarzocco * pytestmark --- .../components/lamarzocco/icons.json | 3 ++ homeassistant/components/lamarzocco/sensor.py | 13 +++++ .../components/lamarzocco/strings.json | 3 ++ tests/components/lamarzocco/conftest.py | 10 ++++ .../lamarzocco/fixtures/config_gs3.json | 2 +- .../snapshots/test_diagnostics.ambr | 4 +- .../lamarzocco/snapshots/test_sensor.ambr | 48 +++++++++++++++++++ .../lamarzocco/test_binary_sensor.py | 2 + tests/components/lamarzocco/test_sensor.py | 2 + 9 files changed, 84 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/lamarzocco/icons.json b/homeassistant/components/lamarzocco/icons.json index a319384d7fd..fb61397575d 100644 --- a/homeassistant/components/lamarzocco/icons.json +++ b/homeassistant/components/lamarzocco/icons.json @@ -82,6 +82,9 @@ "steam_boiler_ready_time": { "default": "mdi:av-timer" }, + "brewing_start_time": { + "default": "mdi:clock-start" + }, "total_coffees_made": { "default": "mdi:coffee" }, diff --git a/homeassistant/components/lamarzocco/sensor.py b/homeassistant/components/lamarzocco/sensor.py index 5dc0eb3dbef..087605315e5 100644 --- a/homeassistant/components/lamarzocco/sensor.py +++ b/homeassistant/components/lamarzocco/sensor.py @@ -11,6 +11,7 @@ from pylamarzocco.models import ( BaseWidgetOutput, CoffeeAndFlushCounter, CoffeeBoiler, + MachineStatus, SteamBoilerLevel, SteamBoilerTemperature, ) @@ -72,6 +73,18 @@ ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = ( in (ModelName.LINEA_MICRA, ModelName.LINEA_MINI_R) ), ), + LaMarzoccoSensorEntityDescription( + key="brewing_start_time", + translation_key="brewing_start_time", + device_class=SensorDeviceClass.TIMESTAMP, + value_fn=( + lambda config: cast( + MachineStatus, config[WidgetType.CM_MACHINE_STATUS] + ).brewing_start_time + ), + entity_category=EntityCategory.DIAGNOSTIC, + available_fn=(lambda coordinator: not coordinator.websocket_terminated), + ), LaMarzoccoSensorEntityDescription( key="steam_boiler_ready_time", translation_key="steam_boiler_ready_time", diff --git a/homeassistant/components/lamarzocco/strings.json b/homeassistant/components/lamarzocco/strings.json index 6383e931c22..8de62efd284 100644 --- a/homeassistant/components/lamarzocco/strings.json +++ b/homeassistant/components/lamarzocco/strings.json @@ -147,6 +147,9 @@ "steam_boiler_ready_time": { "name": "Steam boiler ready time" }, + "brewing_start_time": { + "name": "Brewing start time" + }, "total_coffees_made": { "name": "Total coffees made", "unit_of_measurement": "coffees" diff --git a/tests/components/lamarzocco/conftest.py b/tests/components/lamarzocco/conftest.py index c7530d464db..ccfea1243bc 100644 --- a/tests/components/lamarzocco/conftest.py +++ b/tests/components/lamarzocco/conftest.py @@ -128,3 +128,13 @@ def mock_ble_device() -> BLEDevice: return BLEDevice( "00:00:00:00:00:00", "GS_GS012345", details={"path": "path"}, rssi=50 ) + + +@pytest.fixture +def mock_websocket_terminated() -> Generator[bool]: + """Mock websocket terminated.""" + with patch( + "homeassistant.components.lamarzocco.coordinator.LaMarzoccoUpdateCoordinator.websocket_terminated", + new=False, + ) as mock_websocket_terminated: + yield mock_websocket_terminated diff --git a/tests/components/lamarzocco/fixtures/config_gs3.json b/tests/components/lamarzocco/fixtures/config_gs3.json index 8958bb90fc4..80f535328d5 100644 --- a/tests/components/lamarzocco/fixtures/config_gs3.json +++ b/tests/components/lamarzocco/fixtures/config_gs3.json @@ -25,7 +25,7 @@ "status": "StandBy", "startTime": 1742857195332 }, - "brewingStartTime": null + "brewingStartTime": 1746641060000 }, "tutorialUrl": null }, diff --git a/tests/components/lamarzocco/snapshots/test_diagnostics.ambr b/tests/components/lamarzocco/snapshots/test_diagnostics.ambr index 33b4b4092f7..9dcef0fe0f0 100644 --- a/tests/components/lamarzocco/snapshots/test_diagnostics.ambr +++ b/tests/components/lamarzocco/snapshots/test_diagnostics.ambr @@ -90,7 +90,7 @@ 'BrewingMode', 'StandBy', ]), - 'brewing_start_time': None, + 'brewing_start_time': '2025-05-07T18:04:20+00:00', 'mode': 'BrewingMode', 'next_status': dict({ 'start_time': '2025-03-24T22:59:55.332000+00:00', @@ -297,7 +297,7 @@ 'BrewingMode', 'StandBy', ]), - 'brewing_start_time': None, + 'brewing_start_time': '2025-05-07T18:04:20+00:00', 'mode': 'BrewingMode', 'next_status': dict({ 'start_time': '2025-03-24T22:59:55.332000+00:00', diff --git a/tests/components/lamarzocco/snapshots/test_sensor.ambr b/tests/components/lamarzocco/snapshots/test_sensor.ambr index 46abb93dd2e..15eda23c094 100644 --- a/tests/components/lamarzocco/snapshots/test_sensor.ambr +++ b/tests/components/lamarzocco/snapshots/test_sensor.ambr @@ -1,4 +1,52 @@ # serializer version: 1 +# name: test_sensors[sensor.gs012345_brewing_start_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.gs012345_brewing_start_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Brewing start time', + 'platform': 'lamarzocco', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'brewing_start_time', + 'unique_id': 'GS012345_brewing_start_time', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.gs012345_brewing_start_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'GS012345 Brewing start time', + }), + 'context': , + 'entity_id': 'sensor.gs012345_brewing_start_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2025-05-07T18:04:20+00:00', + }) +# --- # name: test_sensors[sensor.gs012345_coffee_boiler_ready_time-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/lamarzocco/test_binary_sensor.py b/tests/components/lamarzocco/test_binary_sensor.py index 570b5aef8ec..47e5a96ecbc 100644 --- a/tests/components/lamarzocco/test_binary_sensor.py +++ b/tests/components/lamarzocco/test_binary_sensor.py @@ -17,6 +17,8 @@ from . import async_init_integration from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform +pytestmark = pytest.mark.usefixtures("mock_websocket_terminated") + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_binary_sensors( diff --git a/tests/components/lamarzocco/test_sensor.py b/tests/components/lamarzocco/test_sensor.py index 0b050dd7788..d3aba1ef370 100644 --- a/tests/components/lamarzocco/test_sensor.py +++ b/tests/components/lamarzocco/test_sensor.py @@ -14,6 +14,8 @@ from . import async_init_integration from tests.common import MockConfigEntry, snapshot_platform +pytestmark = pytest.mark.usefixtures("mock_websocket_terminated") + async def test_sensors( hass: HomeAssistant,