Add connectivity binary sensor to Home Connect (#138795)

Add connectivity binary sensor
This commit is contained in:
J. Diego Rodríguez Royo 2025-02-18 22:08:40 +01:00 committed by GitHub
parent 1af8b69dd6
commit 8e887f550e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 2 deletions

View File

@ -3,7 +3,7 @@
from dataclasses import dataclass
from typing import cast
from aiohomeconnect.model import StatusKey
from aiohomeconnect.model import EventKey, StatusKey
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.binary_sensor import (
@ -12,6 +12,7 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.components.script import scripts_with_entity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@ -131,13 +132,22 @@ BINARY_SENSORS = (
),
)
CONNECTED_BINARY_ENTITY_DESCRIPTION = BinarySensorEntityDescription(
key=EventKey.BSH_COMMON_APPLIANCE_CONNECTED,
device_class=BinarySensorDeviceClass.CONNECTIVITY,
)
def _get_entities_for_appliance(
entry: HomeConnectConfigEntry,
appliance: HomeConnectApplianceData,
) -> list[HomeConnectEntity]:
"""Get a list of entities."""
entities: list[HomeConnectEntity] = []
entities: list[HomeConnectEntity] = [
HomeConnectConnectivityBinarySensor(
entry.runtime_data, appliance, CONNECTED_BINARY_ENTITY_DESCRIPTION
)
]
entities.extend(
HomeConnectBinarySensor(entry.runtime_data, appliance, description)
for description in BINARY_SENSORS
@ -177,6 +187,21 @@ class HomeConnectBinarySensor(HomeConnectEntity, BinarySensorEntity):
self._attr_is_on = None
class HomeConnectConnectivityBinarySensor(HomeConnectEntity, BinarySensorEntity):
"""Binary sensor for Home Connect appliance's connection status."""
_attr_entity_category = EntityCategory.DIAGNOSTIC
def update_native_value(self) -> None:
"""Set the native value of the binary sensor."""
self._attr_is_on = self.appliance.info.connected
@property
def available(self) -> bool:
"""Return the availability."""
return self.coordinator.last_update_success
class HomeConnectDoorBinarySensor(HomeConnectBinarySensor):
"""Binary sensor for Home Connect Generic Door."""

View File

@ -346,6 +346,49 @@ async def test_binary_sensors_functionality(
assert hass.states.is_state(entity_id, expected)
async def test_connected_sensor_functionality(
hass: HomeAssistant,
config_entry: MockConfigEntry,
integration_setup: Callable[[MagicMock], Awaitable[bool]],
setup_credentials: None,
client: MagicMock,
appliance_ha_id: str,
) -> None:
"""Test if the connected binary sensor reports the right values."""
entity_id = "binary_sensor.washer_connectivity"
assert config_entry.state == ConfigEntryState.NOT_LOADED
assert await integration_setup(client)
assert config_entry.state == ConfigEntryState.LOADED
assert hass.states.is_state(entity_id, STATE_ON)
await client.add_events(
[
EventMessage(
appliance_ha_id,
EventType.DISCONNECTED,
ArrayOfEvents([]),
)
]
)
await hass.async_block_till_done()
assert hass.states.is_state(entity_id, STATE_OFF)
await client.add_events(
[
EventMessage(
appliance_ha_id,
EventType.CONNECTED,
ArrayOfEvents([]),
)
]
)
await hass.async_block_till_done()
assert hass.states.is_state(entity_id, STATE_ON)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_create_issue(
hass: HomeAssistant,