Small WLED cleanups (#52014)

This commit is contained in:
Franck Nijhof 2021-06-19 17:13:48 +02:00 committed by GitHub
parent f550c31886
commit 24c1256c2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 44 deletions

View File

@ -132,27 +132,24 @@ class WLEDMasterLight(WLEDEntity, LightEntity):
@wled_exception_handler
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
data: dict[str, bool | int] = {ATTR_ON: False}
transition = None
if ATTR_TRANSITION in kwargs:
# 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
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
data: dict[str, bool | int] = {ATTR_ON: True}
transition = None
if ATTR_TRANSITION in kwargs:
# 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:
data[ATTR_BRIGHTNESS] = kwargs[ATTR_BRIGHTNESS]
await self.coordinator.wled.master(**data) # type: ignore[arg-type]
await self.coordinator.wled.master(
on=True, brightness=kwargs.get(ATTR_BRIGHTNESS), transition=transition
)
async def async_effect(
self,
@ -171,9 +168,7 @@ class WLEDMasterLight(WLEDEntity, LightEntity):
preset: int,
) -> None:
"""Set a WLED light to a saved preset."""
data = {ATTR_PRESET: preset}
await self.coordinator.wled.preset(**data)
await self.coordinator.wled.preset(preset=preset)
class WLEDSegmentLight(WLEDEntity, LightEntity):
@ -292,22 +287,22 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
@wled_exception_handler
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
data: dict[str, bool | int] = {ATTR_ON: False}
transition = None
if ATTR_TRANSITION in kwargs:
# 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 (
not self._keep_master_light
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
data[ATTR_SEGMENT_ID] = self._segment
await self.coordinator.wled.segment(**data) # type: ignore[arg-type]
await self.coordinator.wled.segment(
segment_id=self._segment, on=False, transition=transition
)
@wled_exception_handler
async def async_turn_on(self, **kwargs: Any) -> None:
@ -364,24 +359,14 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
speed: int | None = None,
) -> None:
"""Set the effect of a WLED light."""
data: dict[str, bool | int | str | None] = {ATTR_SEGMENT_ID: self._segment}
if effect is not None:
data[ATTR_EFFECT] = effect
if intensity is not None:
data[ATTR_INTENSITY] = intensity
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]
await self.coordinator.wled.segment(
segment_id=self._segment,
effect=effect,
intensity=intensity,
palette=palette,
reverse=reverse,
speed=speed,
)
@wled_exception_handler
async def async_preset(
@ -389,9 +374,7 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
preset: int,
) -> None:
"""Set a WLED light to a saved preset."""
data = {ATTR_PRESET: preset}
await self.coordinator.wled.preset(**data)
await self.coordinator.wled.preset(preset=preset)
@callback

View File

@ -330,7 +330,7 @@ async def test_light_error(
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
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(
@ -355,7 +355,7 @@ async def test_light_connection_error(
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
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)
@ -425,6 +425,10 @@ async def test_effect_service(
mock_wled.segment.assert_called_with(
segment_id=0,
effect=9,
intensity=None,
palette=None,
reverse=None,
speed=None,
)
await hass.services.async_call(
@ -445,6 +449,8 @@ async def test_effect_service(
reverse=True,
segment_id=0,
speed=100,
effect=None,
palette=None,
)
await hass.services.async_call(
@ -467,6 +473,7 @@ async def test_effect_service(
reverse=True,
segment_id=0,
speed=100,
intensity=None,
)
await hass.services.async_call(
@ -487,6 +494,8 @@ async def test_effect_service(
intensity=200,
segment_id=0,
speed=100,
palette=None,
reverse=None,
)
await hass.services.async_call(
@ -507,6 +516,8 @@ async def test_effect_service(
intensity=200,
reverse=True,
segment_id=0,
palette=None,
speed=None,
)
@ -532,7 +543,9 @@ async def test_effect_service_error(
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
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(