1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Use DataUpdateCoordinator for wemo (#54866)

* Use DataUpdateCoordinator for wemo

* Rename DeviceWrapper->DeviceCoordinator and make it a subclass of DataUpdateCoordinator

* Rename async_update_data->_async_update_data to override base class

* Rename: device -> coordinator
This commit is contained in:
Eric Severance
2021-08-21 11:14:55 -07:00
committed by GitHub
parent 6cefd558d8
commit 67d04b6082
19 changed files with 507 additions and 629 deletions

View File

@@ -1,5 +1,5 @@
"""Tests for the Wemo light entity via the bridge."""
from unittest.mock import create_autospec, patch
from unittest.mock import create_autospec
import pytest
import pywemo
@@ -8,10 +8,9 @@ from homeassistant.components.homeassistant import (
DOMAIN as HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
)
from homeassistant.components.wemo.light import MIN_TIME_BETWEEN_SCANS
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from . import entity_test_helpers
@@ -32,60 +31,53 @@ def pywemo_bridge_light_fixture(pywemo_device):
light.uniqueID = pywemo_device.serialnumber
light.name = pywemo_device.name
light.bridge = pywemo_device
light.state = {"onoff": 0}
light.state = {"onoff": 0, "available": True}
pywemo_device.Lights = {pywemo_device.serialnumber: light}
return light
def _bypass_throttling():
"""Bypass the util.Throttle on the update_lights method."""
utcnow = dt_util.utcnow()
def increment_and_return_time():
nonlocal utcnow
utcnow += MIN_TIME_BETWEEN_SCANS
return utcnow
return patch("homeassistant.util.utcnow", side_effect=increment_and_return_time)
async def test_async_update_locked_callback_and_update(
hass, pywemo_bridge_light, wemo_entity, pywemo_device
):
"""Test that a callback and a state update request can't both happen at the same time."""
await entity_test_helpers.test_async_update_locked_callback_and_update(
hass,
pywemo_device,
wemo_entity,
)
async def test_async_update_locked_multiple_updates(
hass, pywemo_registry, pywemo_bridge_light, wemo_entity, pywemo_device
hass, pywemo_bridge_light, wemo_entity, pywemo_device
):
"""Test that two state updates do not proceed at the same time."""
pywemo_device.bridge_update.reset_mock()
with _bypass_throttling():
await entity_test_helpers.test_async_update_locked_multiple_updates(
hass,
pywemo_registry,
wemo_entity,
pywemo_device,
update_polling_method=pywemo_device.bridge_update,
)
await entity_test_helpers.test_async_update_locked_multiple_updates(
hass,
pywemo_device,
wemo_entity,
)
async def test_async_update_with_timeout_and_recovery(
async def test_async_update_locked_multiple_callbacks(
hass, pywemo_bridge_light, wemo_entity, pywemo_device
):
"""Test that the entity becomes unavailable after a timeout, and that it recovers."""
with _bypass_throttling():
await entity_test_helpers.test_async_update_with_timeout_and_recovery(
hass, wemo_entity, pywemo_device
)
"""Test that two device callback state updates do not proceed at the same time."""
await entity_test_helpers.test_async_update_locked_multiple_callbacks(
hass,
pywemo_device,
wemo_entity,
)
async def test_async_locked_update_with_exception(
hass, pywemo_bridge_light, wemo_entity, pywemo_device
async def test_available_after_update(
hass, pywemo_registry, pywemo_device, pywemo_bridge_light, wemo_entity
):
"""Test that the entity becomes unavailable when communication is lost."""
with _bypass_throttling():
await entity_test_helpers.test_async_locked_update_with_exception(
hass,
wemo_entity,
pywemo_device,
update_polling_method=pywemo_device.bridge_update,
)
"""Test the avaliability when an On call fails and after an update."""
pywemo_bridge_light.turn_on.side_effect = pywemo.exceptions.ActionException
pywemo_bridge_light.state["onoff"] = 1
await entity_test_helpers.test_avaliable_after_update(
hass, pywemo_registry, pywemo_device, wemo_entity, LIGHT_DOMAIN
)
async def test_light_update_entity(
@@ -95,7 +87,7 @@ async def test_light_update_entity(
await async_setup_component(hass, HA_DOMAIN, {})
# On state.
pywemo_bridge_light.state = {"onoff": 1}
pywemo_bridge_light.state["onoff"] = 1
await hass.services.async_call(
HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
@@ -105,7 +97,7 @@ async def test_light_update_entity(
assert hass.states.get(wemo_entity.entity_id).state == STATE_ON
# Off state.
pywemo_bridge_light.state = {"onoff": 0}
pywemo_bridge_light.state["onoff"] = 0
await hass.services.async_call(
HA_DOMAIN,
SERVICE_UPDATE_ENTITY,