Skip to content

Commit 0c4f232

Browse files
authored
Use public API for UniFi Protect light brightness control (home-assistant#157550)
1 parent 81f4456 commit 0c4f232

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

homeassistant/components/unifiprotect/light.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from typing import Any
77

88
from uiprotect.data import Light, ModelType, ProtectAdoptableDeviceModel
9+
from uiprotect.data.devices import LightDeviceSettings
910

10-
from homeassistant.components.light import ColorMode, LightEntity
11+
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
1112
from homeassistant.core import HomeAssistant, callback
1213
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1314

@@ -72,10 +73,36 @@ def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:
7273

7374
async def async_turn_on(self, **kwargs: Any) -> None:
7475
"""Turn the light on."""
75-
_LOGGER.debug("Turning on light")
76-
await self.device.api.set_light_is_led_force_on(self.device.id, True)
76+
brightness = kwargs.get(ATTR_BRIGHTNESS)
77+
led_level: int | None = None
78+
if brightness is not None:
79+
led_level = hass_to_unifi_brightness(brightness)
80+
_LOGGER.debug(
81+
"Turning on light with brightness %s (led_level=%s)",
82+
brightness,
83+
led_level,
84+
)
85+
else:
86+
_LOGGER.debug("Turning on light")
87+
88+
await self.device.api.update_light_public(
89+
self.device.id,
90+
is_light_force_enabled=True,
91+
light_device_settings=(
92+
LightDeviceSettings(
93+
is_indicator_enabled=self.device.light_device_settings.is_indicator_enabled,
94+
led_level=led_level,
95+
pir_duration=self.device.light_device_settings.pir_duration,
96+
pir_sensitivity=self.device.light_device_settings.pir_sensitivity,
97+
)
98+
if led_level is not None
99+
else None
100+
),
101+
)
77102

78103
async def async_turn_off(self, **kwargs: Any) -> None:
79104
"""Turn the light off."""
80105
_LOGGER.debug("Turning off light")
81-
await self.device.api.set_light_is_led_force_on(self.device.id, False)
106+
await self.device.api.update_light_public(
107+
self.device.id, is_light_force_enabled=False
108+
)

tests/components/unifiprotect/test_light.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def test_light_turn_on(
9898
"""Test light entity turn on."""
9999

100100
light._api = ufp.api
101-
light.api.set_light_is_led_force_on = AsyncMock()
101+
light.api.update_light_public = AsyncMock()
102102

103103
await init_entry(hass, ufp, [light, unadopted_light])
104104
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
@@ -108,8 +108,36 @@ async def test_light_turn_on(
108108
"light", "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True
109109
)
110110

111-
assert light.api.set_light_is_led_force_on.called
112-
assert light.api.set_light_is_led_force_on.call_args == ((light.id, True),)
111+
assert light.api.update_light_public.called
112+
light.api.update_light_public.assert_called_once_with(
113+
light.id, is_light_force_enabled=True, light_device_settings=None
114+
)
115+
116+
117+
async def test_light_turn_on_with_brightness(
118+
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
119+
) -> None:
120+
"""Test light entity turn on with brightness."""
121+
122+
light._api = ufp.api
123+
light.api.update_light_public = AsyncMock()
124+
125+
await init_entry(hass, ufp, [light, unadopted_light])
126+
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
127+
128+
entity_id = "light.test_light"
129+
await hass.services.async_call(
130+
"light",
131+
"turn_on",
132+
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 128},
133+
blocking=True,
134+
)
135+
136+
assert light.api.update_light_public.called
137+
call_kwargs = light.api.update_light_public.call_args[1]
138+
assert call_kwargs["is_light_force_enabled"] is True
139+
assert call_kwargs["light_device_settings"] is not None
140+
assert call_kwargs["light_device_settings"].led_level == 3 # 128/255 * 6 ≈ 3
113141

114142

115143
async def test_light_turn_off(
@@ -118,7 +146,7 @@ async def test_light_turn_off(
118146
"""Test light entity turn off."""
119147

120148
light._api = ufp.api
121-
light.api.set_light_is_led_force_on = AsyncMock()
149+
light.api.update_light_public = AsyncMock()
122150

123151
await init_entry(hass, ufp, [light, unadopted_light])
124152
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
@@ -128,5 +156,7 @@ async def test_light_turn_off(
128156
"light", "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
129157
)
130158

131-
assert light.api.set_light_is_led_force_on.called
132-
assert light.api.set_light_is_led_force_on.call_args == ((light.id, False),)
159+
assert light.api.update_light_public.called
160+
light.api.update_light_public.assert_called_once_with(
161+
light.id, is_light_force_enabled=False
162+
)

0 commit comments

Comments
 (0)