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