From 8e887f550ee73f64e848ab65dfdb027c4f660f0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=2E=20Diego=20Rodr=C3=ADguez=20Royo?=
 <jdrr1998@hotmail.com>
Date: Tue, 18 Feb 2025 22:08:40 +0100
Subject: [PATCH] Add connectivity binary sensor to Home Connect (#138795)

Add connectivity binary sensor
---
 .../components/home_connect/binary_sensor.py  | 29 ++++++++++++-
 .../home_connect/test_binary_sensor.py        | 43 +++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/homeassistant/components/home_connect/binary_sensor.py b/homeassistant/components/home_connect/binary_sensor.py
index 3b2c7c23d68..57ede4b2ff4 100644
--- a/homeassistant/components/home_connect/binary_sensor.py
+++ b/homeassistant/components/home_connect/binary_sensor.py
@@ -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."""
 
diff --git a/tests/components/home_connect/test_binary_sensor.py b/tests/components/home_connect/test_binary_sensor.py
index 211192f592b..a06e386b84f 100644
--- a/tests/components/home_connect/test_binary_sensor.py
+++ b/tests/components/home_connect/test_binary_sensor.py
@@ -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,
-- 
GitLab