Migrate WLED to use DataUpdateCoordinator (#32565)

* Migrate WLED to use DataUpdateCoordinator

* Remove stale debug statement

* Process review suggestions

* Improve tests

* Improve tests
This commit is contained in:
Franck Nijhof authored and GitHub committed 2020-03-13 13:19:05 +01:00
1 parent 26d7b2164e
commit 992daa4a44
8 files changed
+478 -560

No files matched your search

-29
View File
@@ -1,10 +1,8 @@
"""Tests for the WLED integration."""
import aiohttp
from asynctest import patch
from homeassistant.components.wled.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
from homeassistant.const import STATE_ON
from homeassistant.core import HomeAssistant
from tests.components.wled import init_integration
@@ -39,30 +37,3 @@ async def test_setting_unique_id(hass, aioclient_mock):
assert hass.data[DOMAIN]
assert entry.unique_id == "aabbccddeeff"
async def test_interval_update(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the WLED configuration entry unloading."""
entry = await init_integration(hass, aioclient_mock, skip_setup=True)
interval_action = False
def async_track_time_interval(hass, action, interval):
nonlocal interval_action
interval_action = action
with patch(
"homeassistant.components.wled.async_track_time_interval",
new=async_track_time_interval,
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert interval_action
await interval_action() # pylint: disable=not-callable
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_ON
+114 -101
View File
@@ -1,5 +1,6 @@
"""Tests for the WLED light platform."""
import aiohttp
from asynctest.mock import patch
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@@ -23,7 +24,6 @@ from homeassistant.const import (
ATTR_ICON,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
)
@@ -79,82 +79,98 @@ async def test_rgb_light_state(
async def test_switch_change_state(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None:
"""Test the change of state of the WLED switches."""
await init_integration(hass, aioclient_mock)
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_ON
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light", ATTR_TRANSITION: 5},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
on=False, segment_id=0, transition=50,
)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light", ATTR_TRANSITION: 5},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_OFF
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_BRIGHTNESS: 42,
ATTR_EFFECT: "Chase",
ATTR_ENTITY_ID: "light.wled_rgb_light",
ATTR_RGB_COLOR: [255, 0, 0],
ATTR_TRANSITION: 5,
},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
brightness=42,
color_primary=(255, 0, 0),
effect="Chase",
on=True,
segment_id=0,
transition=50,
)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_BRIGHTNESS: 42,
ATTR_EFFECT: "Chase",
ATTR_ENTITY_ID: "light.wled_rgb_light",
ATTR_RGB_COLOR: [255, 0, 0],
ATTR_TRANSITION: 5,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 42
assert state.attributes.get(ATTR_EFFECT) == "Chase"
assert state.attributes.get(ATTR_HS_COLOR) == (0.0, 100.0)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgb_light", ATTR_COLOR_TEMP: 400},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_HS_COLOR) == (28.874, 72.522)
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgb_light", ATTR_COLOR_TEMP: 400},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
color_primary=(255, 159, 70), on=True, segment_id=0,
)
async def test_light_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None:
"""Test error handling of the WLED lights."""
aioclient_mock.post("http://example.local:80/json/state", text="", status=400)
await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
async def test_light_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", exc=aiohttp.ClientError)
await init_integration(hass, aioclient_mock)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_UNAVAILABLE
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgb_light_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light_1")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("light.wled_rgb_light")
assert state.state == STATE_UNAVAILABLE
async def test_rgbw_light(
@@ -168,45 +184,42 @@ async def test_rgbw_light(
assert state.attributes.get(ATTR_HS_COLOR) == (0.0, 100.0)
assert state.attributes.get(ATTR_WHITE_VALUE) == 139
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgbw_light", ATTR_COLOR_TEMP: 400},
blocking=True,
)
await hass.async_block_till_done()
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgbw_light", ATTR_COLOR_TEMP: 400},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
on=True, segment_id=0, color_primary=(255, 159, 70, 139),
)
state = hass.states.get("light.wled_rgbw_light")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_HS_COLOR) == (28.874, 72.522)
assert state.attributes.get(ATTR_WHITE_VALUE) == 139
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgbw_light", ATTR_WHITE_VALUE: 100},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
color_primary=(255, 0, 0, 100), on=True, segment_id=0,
)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wled_rgbw_light", ATTR_WHITE_VALUE: 100},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgbw_light")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_HS_COLOR) == (28.874, 72.522)
assert state.attributes.get(ATTR_WHITE_VALUE) == 100
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "light.wled_rgbw_light",
ATTR_RGB_COLOR: (255, 255, 255),
ATTR_WHITE_VALUE: 100,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgbw_light")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_HS_COLOR) == (0, 0)
assert state.attributes.get(ATTR_WHITE_VALUE) == 100
with patch("wled.WLED.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "light.wled_rgbw_light",
ATTR_RGB_COLOR: (255, 255, 255),
ATTR_WHITE_VALUE: 100,
},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
color_primary=(0, 0, 0, 100), on=True, segment_id=0,
)
+86 -97
View File
@@ -1,5 +1,6 @@
"""Tests for the WLED switch platform."""
import aiohttp
from asynctest.mock import patch
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.wled.const import (
@@ -71,117 +72,105 @@ async def test_switch_change_state(
await init_integration(hass, aioclient_mock)
# Nightlight
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_OFF
with patch("wled.WLED.nightlight") as nightlight_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
nightlight_mock.assert_called_once_with(on=True)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_OFF
with patch("wled.WLED.nightlight") as nightlight_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
nightlight_mock.assert_called_once_with(on=False)
# Sync send
state = hass.states.get("switch.wled_rgb_light_sync_send")
assert state.state == STATE_OFF
with patch("wled.WLED.sync") as sync_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_send"},
blocking=True,
)
await hass.async_block_till_done()
sync_mock.assert_called_once_with(send=True)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_send"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_send")
assert state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_send"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_send")
assert state.state == STATE_OFF
with patch("wled.WLED.sync") as sync_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_send"},
blocking=True,
)
await hass.async_block_till_done()
sync_mock.assert_called_once_with(send=False)
# Sync receive
state = hass.states.get("switch.wled_rgb_light_sync_receive")
assert state.state == STATE_ON
with patch("wled.WLED.sync") as sync_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_receive"},
blocking=True,
)
await hass.async_block_till_done()
sync_mock.assert_called_once_with(receive=False)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_receive"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_receive")
assert state.state == STATE_OFF
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_receive"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_receive")
assert state.state == STATE_ON
with patch("wled.WLED.sync") as sync_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_receive"},
blocking=True,
)
await hass.async_block_till_done()
sync_mock.assert_called_once_with(receive=True)
async def test_switch_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None:
"""Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", text="", status=400)
await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_OFF
assert "Invalid response from API" in caplog.text
async def test_switch_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", exc=aiohttp.ClientError)
await init_integration(hass, aioclient_mock)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_UNAVAILABLE
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_send"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_send")
assert state.state == STATE_UNAVAILABLE
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_sync_receive"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_sync_receive")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state.state == STATE_UNAVAILABLE