mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Small WLED cleanups (#52014)
This commit is contained in:
parent
f550c31886
commit
24c1256c2c
@ -132,27 +132,24 @@ class WLEDMasterLight(WLEDEntity, LightEntity):
|
|||||||
@wled_exception_handler
|
@wled_exception_handler
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn off the light."""
|
"""Turn off the light."""
|
||||||
data: dict[str, bool | int] = {ATTR_ON: False}
|
transition = None
|
||||||
|
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
# WLED uses 100ms per unit, so 10 = 1 second.
|
# WLED uses 100ms per unit, so 10 = 1 second.
|
||||||
data[ATTR_TRANSITION] = round(kwargs[ATTR_TRANSITION] * 10)
|
transition = round(kwargs[ATTR_TRANSITION] * 10)
|
||||||
|
|
||||||
await self.coordinator.wled.master(**data) # type: ignore[arg-type]
|
await self.coordinator.wled.master(on=False, transition=transition)
|
||||||
|
|
||||||
@wled_exception_handler
|
@wled_exception_handler
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on the light."""
|
"""Turn on the light."""
|
||||||
data: dict[str, bool | int] = {ATTR_ON: True}
|
transition = None
|
||||||
|
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
# WLED uses 100ms per unit, so 10 = 1 second.
|
# WLED uses 100ms per unit, so 10 = 1 second.
|
||||||
data[ATTR_TRANSITION] = round(kwargs[ATTR_TRANSITION] * 10)
|
transition = round(kwargs[ATTR_TRANSITION] * 10)
|
||||||
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
await self.coordinator.wled.master(
|
||||||
data[ATTR_BRIGHTNESS] = kwargs[ATTR_BRIGHTNESS]
|
on=True, brightness=kwargs.get(ATTR_BRIGHTNESS), transition=transition
|
||||||
|
)
|
||||||
await self.coordinator.wled.master(**data) # type: ignore[arg-type]
|
|
||||||
|
|
||||||
async def async_effect(
|
async def async_effect(
|
||||||
self,
|
self,
|
||||||
@ -171,9 +168,7 @@ class WLEDMasterLight(WLEDEntity, LightEntity):
|
|||||||
preset: int,
|
preset: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a WLED light to a saved preset."""
|
"""Set a WLED light to a saved preset."""
|
||||||
data = {ATTR_PRESET: preset}
|
await self.coordinator.wled.preset(preset=preset)
|
||||||
|
|
||||||
await self.coordinator.wled.preset(**data)
|
|
||||||
|
|
||||||
|
|
||||||
class WLEDSegmentLight(WLEDEntity, LightEntity):
|
class WLEDSegmentLight(WLEDEntity, LightEntity):
|
||||||
@ -292,22 +287,22 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
|
|||||||
@wled_exception_handler
|
@wled_exception_handler
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn off the light."""
|
"""Turn off the light."""
|
||||||
data: dict[str, bool | int] = {ATTR_ON: False}
|
transition = None
|
||||||
|
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
# WLED uses 100ms per unit, so 10 = 1 second.
|
# WLED uses 100ms per unit, so 10 = 1 second.
|
||||||
data[ATTR_TRANSITION] = round(kwargs[ATTR_TRANSITION] * 10)
|
transition = round(kwargs[ATTR_TRANSITION] * 10)
|
||||||
|
|
||||||
# If there is a single segment, control via the master
|
# If there is a single segment, control via the master
|
||||||
if (
|
if (
|
||||||
not self._keep_master_light
|
not self._keep_master_light
|
||||||
and len(self.coordinator.data.state.segments) == 1
|
and len(self.coordinator.data.state.segments) == 1
|
||||||
):
|
):
|
||||||
await self.coordinator.wled.master(**data) # type: ignore[arg-type]
|
await self.coordinator.wled.master(on=False, transition=transition)
|
||||||
return
|
return
|
||||||
|
|
||||||
data[ATTR_SEGMENT_ID] = self._segment
|
await self.coordinator.wled.segment(
|
||||||
await self.coordinator.wled.segment(**data) # type: ignore[arg-type]
|
segment_id=self._segment, on=False, transition=transition
|
||||||
|
)
|
||||||
|
|
||||||
@wled_exception_handler
|
@wled_exception_handler
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
@ -364,24 +359,14 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
|
|||||||
speed: int | None = None,
|
speed: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the effect of a WLED light."""
|
"""Set the effect of a WLED light."""
|
||||||
data: dict[str, bool | int | str | None] = {ATTR_SEGMENT_ID: self._segment}
|
await self.coordinator.wled.segment(
|
||||||
|
segment_id=self._segment,
|
||||||
if effect is not None:
|
effect=effect,
|
||||||
data[ATTR_EFFECT] = effect
|
intensity=intensity,
|
||||||
|
palette=palette,
|
||||||
if intensity is not None:
|
reverse=reverse,
|
||||||
data[ATTR_INTENSITY] = intensity
|
speed=speed,
|
||||||
|
)
|
||||||
if palette is not None:
|
|
||||||
data[ATTR_PALETTE] = palette
|
|
||||||
|
|
||||||
if reverse is not None:
|
|
||||||
data[ATTR_REVERSE] = reverse
|
|
||||||
|
|
||||||
if speed is not None:
|
|
||||||
data[ATTR_SPEED] = speed
|
|
||||||
|
|
||||||
await self.coordinator.wled.segment(**data) # type: ignore[arg-type]
|
|
||||||
|
|
||||||
@wled_exception_handler
|
@wled_exception_handler
|
||||||
async def async_preset(
|
async def async_preset(
|
||||||
@ -389,9 +374,7 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
|
|||||||
preset: int,
|
preset: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a WLED light to a saved preset."""
|
"""Set a WLED light to a saved preset."""
|
||||||
data = {ATTR_PRESET: preset}
|
await self.coordinator.wled.preset(preset=preset)
|
||||||
|
|
||||||
await self.coordinator.wled.preset(**data)
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -330,7 +330,7 @@ async def test_light_error(
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
assert "Invalid response from API" in caplog.text
|
assert "Invalid response from API" in caplog.text
|
||||||
assert mock_wled.segment.call_count == 1
|
assert mock_wled.segment.call_count == 1
|
||||||
mock_wled.segment.assert_called_with(on=False, segment_id=0)
|
mock_wled.segment.assert_called_with(on=False, segment_id=0, transition=None)
|
||||||
|
|
||||||
|
|
||||||
async def test_light_connection_error(
|
async def test_light_connection_error(
|
||||||
@ -355,7 +355,7 @@ async def test_light_connection_error(
|
|||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
assert "Error communicating with API" in caplog.text
|
assert "Error communicating with API" in caplog.text
|
||||||
assert mock_wled.segment.call_count == 1
|
assert mock_wled.segment.call_count == 1
|
||||||
mock_wled.segment.assert_called_with(on=False, segment_id=0)
|
mock_wled.segment.assert_called_with(on=False, segment_id=0, transition=None)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("mock_wled", ["wled/rgbw.json"], indirect=True)
|
@pytest.mark.parametrize("mock_wled", ["wled/rgbw.json"], indirect=True)
|
||||||
@ -425,6 +425,10 @@ async def test_effect_service(
|
|||||||
mock_wled.segment.assert_called_with(
|
mock_wled.segment.assert_called_with(
|
||||||
segment_id=0,
|
segment_id=0,
|
||||||
effect=9,
|
effect=9,
|
||||||
|
intensity=None,
|
||||||
|
palette=None,
|
||||||
|
reverse=None,
|
||||||
|
speed=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
@ -445,6 +449,8 @@ async def test_effect_service(
|
|||||||
reverse=True,
|
reverse=True,
|
||||||
segment_id=0,
|
segment_id=0,
|
||||||
speed=100,
|
speed=100,
|
||||||
|
effect=None,
|
||||||
|
palette=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
@ -467,6 +473,7 @@ async def test_effect_service(
|
|||||||
reverse=True,
|
reverse=True,
|
||||||
segment_id=0,
|
segment_id=0,
|
||||||
speed=100,
|
speed=100,
|
||||||
|
intensity=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
@ -487,6 +494,8 @@ async def test_effect_service(
|
|||||||
intensity=200,
|
intensity=200,
|
||||||
segment_id=0,
|
segment_id=0,
|
||||||
speed=100,
|
speed=100,
|
||||||
|
palette=None,
|
||||||
|
reverse=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
@ -507,6 +516,8 @@ async def test_effect_service(
|
|||||||
intensity=200,
|
intensity=200,
|
||||||
reverse=True,
|
reverse=True,
|
||||||
segment_id=0,
|
segment_id=0,
|
||||||
|
palette=None,
|
||||||
|
speed=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -532,7 +543,9 @@ async def test_effect_service_error(
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
assert "Invalid response from API" in caplog.text
|
assert "Invalid response from API" in caplog.text
|
||||||
assert mock_wled.segment.call_count == 1
|
assert mock_wled.segment.call_count == 1
|
||||||
mock_wled.segment.assert_called_with(effect=9, segment_id=0)
|
mock_wled.segment.assert_called_with(
|
||||||
|
effect=9, segment_id=0, intensity=None, palette=None, reverse=None, speed=None
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_preset_service(
|
async def test_preset_service(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user