Add websocket connectivity binary sensor to lamarzocco (#143161)

This commit is contained in:
Josef Zweck 2025-04-19 11:52:56 +02:00 committed by GitHub
parent b6e9643586
commit 9c9c115d1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 9 deletions

View File

@ -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)

View File

@ -83,6 +83,9 @@
},
"water_tank": {
"name": "Water tank empty"
},
"websocket_connected": {
"name": "WebSocket connected"
}
},
"button": {

View File

@ -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': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.gs012345_websocket_connected',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>,
'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': <ANY>,
'entity_id': 'binary_sensor.gs012345_websocket_connected',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -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,