From 89a6640b82a39602142cc1cc9ebb1fa057a172c9 Mon Sep 17 00:00:00 2001
From: Marcel van der Veldt <m.vanderveldt@outlook.com>
Date: Tue, 14 Dec 2021 01:23:32 +0100
Subject: [PATCH] Blacklist availability check for a light at startup in Hue
 integration (#61737)

---
 homeassistant/components/hue/v2/entity.py | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/homeassistant/components/hue/v2/entity.py b/homeassistant/components/hue/v2/entity.py
index 68c427fd3a5..ae345238c23 100644
--- a/homeassistant/components/hue/v2/entity.py
+++ b/homeassistant/components/hue/v2/entity.py
@@ -47,6 +47,20 @@ class HueBaseEntity(Entity):
             self._attr_device_info = DeviceInfo(
                 identifiers={(DOMAIN, self.device.id)},
             )
+        # some (3th party) Hue lights report their connection status incorrectly
+        # causing the zigbee availability to report as disconnected while in fact
+        # it can be controlled. Although this is in fact something the device manufacturer
+        # should fix, we work around it here. If the light is reported unavailable at
+        # startup, we ignore the availability status of the zigbee connection
+        self._ignore_availability = False
+        if self.device is None:
+            return
+        if zigbee := self.bridge.api.devices.get_zigbee_connectivity(self.device.id):
+            self._ignore_availability = (
+                # Official Hue lights are reliable
+                self.device.product_data.manufacturer_name != "Signify Netherlands B.V."
+                and zigbee.status != ConnectivityServiceStatus.CONNECTED
+            )
 
     @property
     def name(self) -> str:
@@ -98,13 +112,12 @@ class HueBaseEntity(Entity):
     def available(self) -> bool:
         """Return entity availability."""
         if self.device is None:
-            # devices without a device attached should be always available
+            # entities without a device attached should be always available
             return True
         if self.resource.type == ResourceTypes.ZIGBEE_CONNECTIVITY:
             # the zigbee connectivity sensor itself should be always available
             return True
-        if self.device.product_data.manufacturer_name != "Signify Netherlands B.V.":
-            # availability status for non-philips brand lights is unreliable
+        if self._ignore_availability:
             return True
         if zigbee := self.bridge.api.devices.get_zigbee_connectivity(self.device.id):
             # all device-attached entities get availability from the zigbee connectivity