mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
UnifiProtect Refactor light control methods to use new API (#134625)
This commit is contained in:
parent
e38f21c4ef
commit
f1c62000e1
@ -7,7 +7,7 @@ from typing import Any
|
|||||||
|
|
||||||
from uiprotect.data import Light, ModelType, ProtectAdoptableDeviceModel
|
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.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
@ -71,13 +71,10 @@ class ProtectLight(ProtectDeviceEntity, LightEntity):
|
|||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
hass_brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
_LOGGER.debug("Turning on light")
|
||||||
unifi_brightness = hass_to_unifi_brightness(hass_brightness)
|
await self.device.api.set_light_is_led_force_on(self.device.id, True)
|
||||||
|
|
||||||
_LOGGER.debug("Turning on light with brightness %s", unifi_brightness)
|
|
||||||
await self.device.set_light(True, unifi_brightness)
|
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light off."""
|
"""Turn the light off."""
|
||||||
_LOGGER.debug("Turning off light")
|
_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)
|
||||||
|
@ -95,42 +95,38 @@ async def test_light_update(
|
|||||||
async def test_light_turn_on(
|
async def test_light_turn_on(
|
||||||
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
|
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
|
||||||
) -> None:
|
) -> 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])
|
await init_entry(hass, ufp, [light, unadopted_light])
|
||||||
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
|
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
|
||||||
|
|
||||||
entity_id = "light.test_light"
|
entity_id = "light.test_light"
|
||||||
light.__pydantic_fields__["set_light"] = Mock(final=False, frozen=False)
|
|
||||||
light.set_light = AsyncMock()
|
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"light",
|
"light", "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||||
"turn_on",
|
|
||||||
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 128},
|
|
||||||
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(
|
async def test_light_turn_off(
|
||||||
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
|
hass: HomeAssistant, ufp: MockUFPFixture, light: Light, unadopted_light: Light
|
||||||
) -> None:
|
) -> 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])
|
await init_entry(hass, ufp, [light, unadopted_light])
|
||||||
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
|
assert_entity_counts(hass, Platform.LIGHT, 1, 1)
|
||||||
|
|
||||||
entity_id = "light.test_light"
|
entity_id = "light.test_light"
|
||||||
light.__pydantic_fields__["set_light"] = Mock(final=False, frozen=False)
|
|
||||||
light.set_light = AsyncMock()
|
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"light",
|
"light", "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||||
"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),)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user