3535 from .hub import UnifiHub
3636
3737
38+ def convert_brightness_to_unifi (ha_brightness : int ) -> int :
39+ """Convert Home Assistant brightness (0-255) to UniFi brightness (0-100)."""
40+ return round ((ha_brightness / 255 ) * 100 )
41+
42+
43+ def convert_brightness_to_ha (
44+ unifi_brightness : int ,
45+ ) -> int :
46+ """Convert UniFi brightness (0-100) to Home Assistant brightness (0-255)."""
47+ return round ((unifi_brightness / 100 ) * 255 )
48+
49+
50+ def get_device_brightness_or_default (device : Device ) -> int :
51+ """Get device's current LED brightness. Defaults to 100 (full brightness) if not set."""
52+ value = device .led_override_color_brightness
53+ return value if value is not None else 100
54+
55+
3856@callback
3957def async_device_led_supported_fn (hub : UnifiHub , obj_id : str ) -> bool :
4058 """Check if device supports LED control."""
4159 device : Device = hub .api .devices [obj_id ]
42- return device .supports_led_ring
60+ return device .led_override is not None or device . supports_led_ring
4361
4462
4563@callback
@@ -56,17 +74,24 @@ async def async_device_led_control_fn(
5674
5775 status = "on" if turn_on else "off"
5876
59- brightness = (
60- int ((kwargs [ATTR_BRIGHTNESS ] / 255 ) * 100 )
61- if ATTR_BRIGHTNESS in kwargs
62- else device .led_override_color_brightness
63- )
77+ # Only send brightness and RGB if device has LED_RING hardware support
78+ if device .supports_led_ring :
79+ # Use provided brightness or fall back to device's current brightness
80+ if ATTR_BRIGHTNESS in kwargs :
81+ brightness = convert_brightness_to_unifi (kwargs [ATTR_BRIGHTNESS ])
82+ else :
83+ brightness = get_device_brightness_or_default (device )
6484
65- color = (
66- f"#{ kwargs [ATTR_RGB_COLOR ][0 ]:02x} { kwargs [ATTR_RGB_COLOR ][1 ]:02x} { kwargs [ATTR_RGB_COLOR ][2 ]:02x} "
67- if ATTR_RGB_COLOR in kwargs
68- else device .led_override_color
69- )
85+ # Use provided RGB color or fall back to device's current color
86+ color : str | None
87+ if ATTR_RGB_COLOR in kwargs :
88+ rgb = kwargs [ATTR_RGB_COLOR ]
89+ color = f"#{ rgb [0 ]:02x} { rgb [1 ]:02x} { rgb [2 ]:02x} "
90+ else :
91+ color = device .led_override_color
92+ else :
93+ brightness = None
94+ color = None
7095
7196 await hub .api .request (
7297 DeviceSetLedStatus .create (
@@ -127,12 +152,19 @@ class UnifiLightEntity[HandlerT: APIHandler, ApiItemT: ApiItem](
127152
128153 entity_description : UnifiLightEntityDescription [HandlerT , ApiItemT ]
129154 _attr_supported_features = LightEntityFeature (0 )
130- _attr_color_mode = ColorMode .RGB
131- _attr_supported_color_modes = {ColorMode .RGB }
132155
133156 @callback
134157 def async_initiate_state (self ) -> None :
135158 """Initiate entity state."""
159+ device = cast (Device , self .entity_description .object_fn (self .api , self ._obj_id ))
160+
161+ if device .supports_led_ring :
162+ self ._attr_supported_color_modes = {ColorMode .RGB }
163+ self ._attr_color_mode = ColorMode .RGB
164+ else :
165+ self ._attr_supported_color_modes = {ColorMode .ONOFF }
166+ self ._attr_color_mode = ColorMode .ONOFF
167+
136168 self .async_update_state (ItemEvent .ADDED , self ._obj_id )
137169
138170 async def async_turn_on (self , ** kwargs : Any ) -> None :
@@ -150,23 +182,24 @@ def async_update_state(self, event: ItemEvent, obj_id: str) -> None:
150182 """Update entity state."""
151183 description = self .entity_description
152184 device_obj = description .object_fn (self .api , self ._obj_id )
153-
154185 device = cast (Device , device_obj )
155186
156187 self ._attr_is_on = description .is_on_fn (self .hub , device_obj )
157188
158- brightness = device .led_override_color_brightness
159- self ._attr_brightness = (
160- int ((int (brightness ) / 100 ) * 255 ) if brightness is not None else None
161- )
162-
163- hex_color = (
164- device .led_override_color .lstrip ("#" )
165- if self ._attr_is_on and device .led_override_color
166- else None
167- )
168- if hex_color and len (hex_color ) == 6 :
169- rgb_list = rgb_hex_to_rgb_list (hex_color )
170- self ._attr_rgb_color = (rgb_list [0 ], rgb_list [1 ], rgb_list [2 ])
171- else :
172- self ._attr_rgb_color = None
189+ # Only set brightness and RGB if device has LED_RING hardware support
190+ if device .supports_led_ring :
191+ self ._attr_brightness = convert_brightness_to_ha (
192+ get_device_brightness_or_default (device )
193+ )
194+
195+ # Parse hex color from device and convert to RGB tuple
196+ hex_color = (
197+ device .led_override_color .lstrip ("#" )
198+ if self ._attr_is_on and device .led_override_color
199+ else None
200+ )
201+ if hex_color and len (hex_color ) == 6 :
202+ rgb_list = rgb_hex_to_rgb_list (hex_color )
203+ self ._attr_rgb_color = (rgb_list [0 ], rgb_list [1 ], rgb_list [2 ])
204+ else :
205+ self ._attr_rgb_color = None
0 commit comments