mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use brightness stored in hardware device when switching LCN lights (#147375)
This commit is contained in:
parent
22e46d9977
commit
520d92b902
@ -18,6 +18,7 @@ from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.color import brightness_to_value, value_to_brightness
|
||||
|
||||
from .const import (
|
||||
CONF_DIMMABLE,
|
||||
@ -29,6 +30,8 @@ from .const import (
|
||||
from .entity import LcnEntity
|
||||
from .helpers import InputType, LcnConfigEntry
|
||||
|
||||
BRIGHTNESS_SCALE = (1, 100)
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@ -91,8 +94,6 @@ class LcnOutputLight(LcnEntity, LightEntity):
|
||||
)
|
||||
self.dimmable = config[CONF_DOMAIN_DATA][CONF_DIMMABLE]
|
||||
|
||||
self._is_dimming_to_zero = False
|
||||
|
||||
if self.dimmable:
|
||||
self._attr_color_mode = ColorMode.BRIGHTNESS
|
||||
else:
|
||||
@ -113,10 +114,6 @@ class LcnOutputLight(LcnEntity, LightEntity):
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
percent = int(kwargs[ATTR_BRIGHTNESS] / 255.0 * 100)
|
||||
else:
|
||||
percent = 100
|
||||
if ATTR_TRANSITION in kwargs:
|
||||
transition = pypck.lcn_defs.time_to_ramp_value(
|
||||
kwargs[ATTR_TRANSITION] * 1000
|
||||
@ -124,12 +121,23 @@ class LcnOutputLight(LcnEntity, LightEntity):
|
||||
else:
|
||||
transition = self._transition
|
||||
|
||||
if not await self.device_connection.dim_output(
|
||||
self.output.value, percent, transition
|
||||
):
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
percent = int(
|
||||
brightness_to_value(BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])
|
||||
)
|
||||
if not await self.device_connection.dim_output(
|
||||
self.output.value, percent, transition
|
||||
):
|
||||
return
|
||||
elif not self.is_on:
|
||||
if not await self.device_connection.toggle_output(
|
||||
self.output.value, transition, to_memory=True
|
||||
):
|
||||
return
|
||||
else:
|
||||
return
|
||||
|
||||
self._attr_is_on = True
|
||||
self._is_dimming_to_zero = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
@ -141,13 +149,13 @@ class LcnOutputLight(LcnEntity, LightEntity):
|
||||
else:
|
||||
transition = self._transition
|
||||
|
||||
if not await self.device_connection.dim_output(
|
||||
self.output.value, 0, transition
|
||||
):
|
||||
return
|
||||
self._is_dimming_to_zero = bool(transition)
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
if self.is_on:
|
||||
if not await self.device_connection.toggle_output(
|
||||
self.output.value, transition, to_memory=True
|
||||
):
|
||||
return
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
def input_received(self, input_obj: InputType) -> None:
|
||||
"""Set light state when LCN input object (command) is received."""
|
||||
@ -157,11 +165,9 @@ class LcnOutputLight(LcnEntity, LightEntity):
|
||||
):
|
||||
return
|
||||
|
||||
self._attr_brightness = int(input_obj.get_percent() / 100.0 * 255)
|
||||
if self._attr_brightness == 0:
|
||||
self._is_dimming_to_zero = False
|
||||
if not self._is_dimming_to_zero and self._attr_brightness is not None:
|
||||
self._attr_is_on = self._attr_brightness > 0
|
||||
percent = input_obj.get_percent()
|
||||
self._attr_brightness = value_to_brightness(BRIGHTNESS_SCALE, percent)
|
||||
self._attr_is_on = bool(percent)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
|
@ -51,9 +51,9 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No
|
||||
"""Test the output light turns on."""
|
||||
await init_integration(hass, entry)
|
||||
|
||||
with patch.object(MockModuleConnection, "dim_output") as dim_output:
|
||||
with patch.object(MockModuleConnection, "toggle_output") as toggle_output:
|
||||
# command failed
|
||||
dim_output.return_value = False
|
||||
toggle_output.return_value = False
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
@ -62,15 +62,15 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
dim_output.assert_awaited_with(0, 100, 9)
|
||||
toggle_output.assert_awaited_with(0, 9, to_memory=True)
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
assert state.state != STATE_ON
|
||||
|
||||
# command success
|
||||
dim_output.reset_mock(return_value=True)
|
||||
dim_output.return_value = True
|
||||
toggle_output.reset_mock(return_value=True)
|
||||
toggle_output.return_value = True
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
@ -79,7 +79,7 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
dim_output.assert_awaited_with(0, 100, 9)
|
||||
toggle_output.assert_awaited_with(0, 9, to_memory=True)
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
@ -117,12 +117,16 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N
|
||||
"""Test the output light turns off."""
|
||||
await init_integration(hass, entry)
|
||||
|
||||
with patch.object(MockModuleConnection, "dim_output") as dim_output:
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
state.state = STATE_ON
|
||||
with patch.object(MockModuleConnection, "toggle_output") as toggle_output:
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: LIGHT_OUTPUT1},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# command failed
|
||||
dim_output.return_value = False
|
||||
toggle_output.return_value = False
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
@ -131,15 +135,15 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
dim_output.assert_awaited_with(0, 0, 9)
|
||||
toggle_output.assert_awaited_with(0, 9, to_memory=True)
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
assert state.state != STATE_OFF
|
||||
|
||||
# command success
|
||||
dim_output.reset_mock(return_value=True)
|
||||
dim_output.return_value = True
|
||||
toggle_output.reset_mock(return_value=True)
|
||||
toggle_output.return_value = True
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
@ -148,36 +152,7 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
dim_output.assert_awaited_with(0, 0, 9)
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_output_turn_off_with_attributes(
|
||||
hass: HomeAssistant, entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test the output light turns off."""
|
||||
await init_integration(hass, entry)
|
||||
|
||||
with patch.object(MockModuleConnection, "dim_output") as dim_output:
|
||||
dim_output.return_value = True
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
state.state = STATE_ON
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_LIGHT,
|
||||
SERVICE_TURN_OFF,
|
||||
{
|
||||
ATTR_ENTITY_ID: LIGHT_OUTPUT1,
|
||||
ATTR_TRANSITION: 2,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
dim_output.assert_awaited_with(0, 0, 6)
|
||||
toggle_output.assert_awaited_with(0, 9, to_memory=True)
|
||||
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
@ -288,7 +263,7 @@ async def test_pushed_output_status_change(
|
||||
state = hass.states.get(LIGHT_OUTPUT1)
|
||||
assert state is not None
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes[ATTR_BRIGHTNESS] == 127
|
||||
assert state.attributes[ATTR_BRIGHTNESS] == 128
|
||||
|
||||
# push status "off"
|
||||
inp = ModStatusOutput(address, 0, 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user