From 9c9c115d1af4ecb236cdf28775cfbd24d1544c91 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Sat, 19 Apr 2025 11:52:56 +0200 Subject: [PATCH] Add websocket connectivity binary sensor to lamarzocco (#143161) --- .../components/lamarzocco/binary_sensor.py | 27 +++++++---- .../components/lamarzocco/strings.json | 3 ++ .../snapshots/test_binary_sensor.ambr | 48 +++++++++++++++++++ .../lamarzocco/test_binary_sensor.py | 2 + 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/lamarzocco/binary_sensor.py b/homeassistant/components/lamarzocco/binary_sensor.py index 2c45104859a..98cf7cf222e 100644 --- a/homeassistant/components/lamarzocco/binary_sensor.py +++ b/homeassistant/components/lamarzocco/binary_sensor.py @@ -4,8 +4,9 @@ from collections.abc import Callable from dataclasses import dataclass from typing import cast +from pylamarzocco import LaMarzoccoMachine from pylamarzocco.const import BackFlushStatus, MachineState, WidgetType -from pylamarzocco.models import BackFlush, BaseWidgetOutput, MachineStatus +from pylamarzocco.models import BackFlush, MachineStatus from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, @@ -30,7 +31,7 @@ class LaMarzoccoBinarySensorEntityDescription( ): """Description of a La Marzocco binary sensor.""" - is_on_fn: Callable[[dict[WidgetType, BaseWidgetOutput]], bool | None] + is_on_fn: Callable[[LaMarzoccoMachine], bool | None] ENTITIES: tuple[LaMarzoccoBinarySensorEntityDescription, ...] = ( @@ -38,7 +39,7 @@ ENTITIES: tuple[LaMarzoccoBinarySensorEntityDescription, ...] = ( key="water_tank", translation_key="water_tank", device_class=BinarySensorDeviceClass.PROBLEM, - is_on_fn=lambda config: WidgetType.CM_NO_WATER in config, + is_on_fn=lambda machine: WidgetType.CM_NO_WATER in machine.dashboard.config, entity_category=EntityCategory.DIAGNOSTIC, ), LaMarzoccoBinarySensorEntityDescription( @@ -46,8 +47,8 @@ ENTITIES: tuple[LaMarzoccoBinarySensorEntityDescription, ...] = ( translation_key="brew_active", device_class=BinarySensorDeviceClass.RUNNING, is_on_fn=( - lambda config: cast( - MachineStatus, config[WidgetType.CM_MACHINE_STATUS] + lambda machine: cast( + MachineStatus, machine.dashboard.config[WidgetType.CM_MACHINE_STATUS] ).status is MachineState.BREWING ), @@ -59,11 +60,21 @@ ENTITIES: tuple[LaMarzoccoBinarySensorEntityDescription, ...] = ( translation_key="backflush_enabled", device_class=BinarySensorDeviceClass.RUNNING, is_on_fn=( - lambda config: cast(BackFlush, config[WidgetType.CM_BACK_FLUSH]).status + lambda machine: cast( + BackFlush, machine.dashboard.config[WidgetType.CM_BACK_FLUSH] + ).status is BackFlushStatus.REQUESTED ), entity_category=EntityCategory.DIAGNOSTIC, ), + LaMarzoccoBinarySensorEntityDescription( + key="websocket_connected", + translation_key="websocket_connected", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + is_on_fn=(lambda machine: machine.websocket.connected), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), ) @@ -90,6 +101,4 @@ class LaMarzoccoBinarySensorEntity(LaMarzoccoEntity, BinarySensorEntity): @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" - return self.entity_description.is_on_fn( - self.coordinator.device.dashboard.config - ) + return self.entity_description.is_on_fn(self.coordinator.device) diff --git a/homeassistant/components/lamarzocco/strings.json b/homeassistant/components/lamarzocco/strings.json index fe7475a23c9..ad58c4e0ee3 100644 --- a/homeassistant/components/lamarzocco/strings.json +++ b/homeassistant/components/lamarzocco/strings.json @@ -83,6 +83,9 @@ }, "water_tank": { "name": "Water tank empty" + }, + "websocket_connected": { + "name": "WebSocket connected" } }, "button": { diff --git a/tests/components/lamarzocco/snapshots/test_binary_sensor.ambr b/tests/components/lamarzocco/snapshots/test_binary_sensor.ambr index 2abf182095e..0e772fb9653 100644 --- a/tests/components/lamarzocco/snapshots/test_binary_sensor.ambr +++ b/tests/components/lamarzocco/snapshots/test_binary_sensor.ambr @@ -143,3 +143,51 @@ 'state': 'off', }) # --- +# name: test_binary_sensors[binary_sensor.gs012345_websocket_connected-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.gs012345_websocket_connected', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'WebSocket connected', + 'platform': 'lamarzocco', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'websocket_connected', + 'unique_id': 'GS012345_websocket_connected', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_sensors[binary_sensor.gs012345_websocket_connected-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'GS012345 WebSocket connected', + }), + 'context': , + 'entity_id': 'binary_sensor.gs012345_websocket_connected', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/lamarzocco/test_binary_sensor.py b/tests/components/lamarzocco/test_binary_sensor.py index bf4c3fc4a33..2fbd58eab85 100644 --- a/tests/components/lamarzocco/test_binary_sensor.py +++ b/tests/components/lamarzocco/test_binary_sensor.py @@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch from freezegun.api import FrozenDateTimeFactory from pylamarzocco.exceptions import RequestNotSuccessful +import pytest from syrupy import SnapshotAssertion from homeassistant.const import STATE_UNAVAILABLE, Platform @@ -16,6 +17,7 @@ from . import async_init_integration from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_binary_sensors( hass: HomeAssistant, mock_config_entry: MockConfigEntry,