Skip to content

Commit 8e887f5

Browse files
authored
Add connectivity binary sensor to Home Connect (#138795)
Add connectivity binary sensor
1 parent 1af8b69 commit 8e887f5

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

homeassistant/components/home_connect/binary_sensor.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dataclasses import dataclass
44
from typing import cast
55

6-
from aiohomeconnect.model import StatusKey
6+
from aiohomeconnect.model import EventKey, StatusKey
77

88
from homeassistant.components.automation import automations_with_entity
99
from homeassistant.components.binary_sensor import (
@@ -12,6 +12,7 @@
1212
BinarySensorEntityDescription,
1313
)
1414
from homeassistant.components.script import scripts_with_entity
15+
from homeassistant.const import EntityCategory
1516
from homeassistant.core import HomeAssistant
1617
from homeassistant.helpers import entity_registry as er
1718
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@@ -131,13 +132,22 @@ class HomeConnectBinarySensorEntityDescription(BinarySensorEntityDescription):
131132
),
132133
)
133134

135+
CONNECTED_BINARY_ENTITY_DESCRIPTION = BinarySensorEntityDescription(
136+
key=EventKey.BSH_COMMON_APPLIANCE_CONNECTED,
137+
device_class=BinarySensorDeviceClass.CONNECTIVITY,
138+
)
139+
134140

135141
def _get_entities_for_appliance(
136142
entry: HomeConnectConfigEntry,
137143
appliance: HomeConnectApplianceData,
138144
) -> list[HomeConnectEntity]:
139145
"""Get a list of entities."""
140-
entities: list[HomeConnectEntity] = []
146+
entities: list[HomeConnectEntity] = [
147+
HomeConnectConnectivityBinarySensor(
148+
entry.runtime_data, appliance, CONNECTED_BINARY_ENTITY_DESCRIPTION
149+
)
150+
]
141151
entities.extend(
142152
HomeConnectBinarySensor(entry.runtime_data, appliance, description)
143153
for description in BINARY_SENSORS
@@ -177,6 +187,21 @@ def update_native_value(self) -> None:
177187
self._attr_is_on = None
178188

179189

190+
class HomeConnectConnectivityBinarySensor(HomeConnectEntity, BinarySensorEntity):
191+
"""Binary sensor for Home Connect appliance's connection status."""
192+
193+
_attr_entity_category = EntityCategory.DIAGNOSTIC
194+
195+
def update_native_value(self) -> None:
196+
"""Set the native value of the binary sensor."""
197+
self._attr_is_on = self.appliance.info.connected
198+
199+
@property
200+
def available(self) -> bool:
201+
"""Return the availability."""
202+
return self.coordinator.last_update_success
203+
204+
180205
class HomeConnectDoorBinarySensor(HomeConnectBinarySensor):
181206
"""Binary sensor for Home Connect Generic Door."""
182207

tests/components/home_connect/test_binary_sensor.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,49 @@ async def test_binary_sensors_functionality(
346346
assert hass.states.is_state(entity_id, expected)
347347

348348

349+
async def test_connected_sensor_functionality(
350+
hass: HomeAssistant,
351+
config_entry: MockConfigEntry,
352+
integration_setup: Callable[[MagicMock], Awaitable[bool]],
353+
setup_credentials: None,
354+
client: MagicMock,
355+
appliance_ha_id: str,
356+
) -> None:
357+
"""Test if the connected binary sensor reports the right values."""
358+
entity_id = "binary_sensor.washer_connectivity"
359+
assert config_entry.state == ConfigEntryState.NOT_LOADED
360+
assert await integration_setup(client)
361+
assert config_entry.state == ConfigEntryState.LOADED
362+
363+
assert hass.states.is_state(entity_id, STATE_ON)
364+
365+
await client.add_events(
366+
[
367+
EventMessage(
368+
appliance_ha_id,
369+
EventType.DISCONNECTED,
370+
ArrayOfEvents([]),
371+
)
372+
]
373+
)
374+
await hass.async_block_till_done()
375+
376+
assert hass.states.is_state(entity_id, STATE_OFF)
377+
378+
await client.add_events(
379+
[
380+
EventMessage(
381+
appliance_ha_id,
382+
EventType.CONNECTED,
383+
ArrayOfEvents([]),
384+
)
385+
]
386+
)
387+
await hass.async_block_till_done()
388+
389+
assert hass.states.is_state(entity_id, STATE_ON)
390+
391+
349392
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
350393
async def test_create_issue(
351394
hass: HomeAssistant,

0 commit comments

Comments
 (0)