Skip to content

Commit

Permalink
UnifiProtect Refactor light control methods to use new API (home-assi…
Browse files Browse the repository at this point in the history
  • Loading branch information
RaHehl authored Jan 6, 2025
1 parent e38f21c commit f1c6200
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
11 changes: 4 additions & 7 deletions homeassistant/components/unifiprotect/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from uiprotect.data import Light, ModelType, ProtectAdoptableDeviceModel

from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.components.light import ColorMode, LightEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand Down Expand Up @@ -71,13 +71,10 @@ def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
hass_brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
unifi_brightness = hass_to_unifi_brightness(hass_brightness)

_LOGGER.debug("Turning on light with brightness %s", unifi_brightness)
await self.device.set_light(True, unifi_brightness)
_LOGGER.debug("Turning on light")
await self.device.api.set_light_is_led_force_on(self.device.id, True)

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
_LOGGER.debug("Turning off light")
await self.device.set_light(False)
await self.device.api.set_light_is_led_force_on(self.device.id, False)
32 changes: 14 additions & 18 deletions tests/components/unifiprotect/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,38 @@ async def test_light_update(
async def test_light_turn_on(
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
) -> None:
"""Test light entity turn off."""
"""Test light entity turn on."""

light._api = ufp.api
light.api.set_light_is_led_force_on = AsyncMock()

await init_entry(hass, ufp, [light, unadopted_light])
assert_entity_counts(hass, Platform.LIGHT, 1, 1)

entity_id = "light.test_light"
light.__pydantic_fields__["set_light"] = Mock(final=False, frozen=False)
light.set_light = AsyncMock()

await hass.services.async_call(
"light",
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 128},
blocking=True,
"light", "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True
)

light.set_light.assert_called_once_with(True, 3)
assert light.api.set_light_is_led_force_on.called
assert light.api.set_light_is_led_force_on.call_args == ((light.id, True),)


async def test_light_turn_off(
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
) -> None:
"""Test light entity turn on."""
"""Test light entity turn off."""

light._api = ufp.api
light.api.set_light_is_led_force_on = AsyncMock()

await init_entry(hass, ufp, [light, unadopted_light])
assert_entity_counts(hass, Platform.LIGHT, 1, 1)

entity_id = "light.test_light"
light.__pydantic_fields__["set_light"] = Mock(final=False, frozen=False)
light.set_light = AsyncMock()

await hass.services.async_call(
"light",
"turn_off",
{ATTR_ENTITY_ID: entity_id},
blocking=True,
"light", "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
)

light.set_light.assert_called_once_with(False)
assert light.api.set_light_is_led_force_on.called
assert light.api.set_light_is_led_force_on.call_args == ((light.id, False),)

0 comments on commit f1c6200

Please sign in to comment.