Update Hue data fetching (#31338)
* Refactor Hue Lights to use DataCoordinator * Redo how Hue updates data * Address comments * Inherit from Entity and remove pylint disable * Add tests for debounce
This commit is contained in:
1 parent
ae76b5be5a
commit
166d770ddd
15 files changed
+549
-355
No files matched your search
@@ -0,0 +1,11 @@
|
||||
"""Test helpers for Hue."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_request_delay():
|
||||
"""Make the request refresh delay 0 for instant tests."""
|
||||
with patch("homeassistant.components.hue.light.REQUEST_REFRESH_DELAY", 0):
|
||||
yield
|
||||
@@ -179,11 +179,13 @@ LIGHT_GAMUT_TYPE = "A"
|
||||
def mock_bridge(hass):
|
||||
"""Mock a Hue bridge."""
|
||||
bridge = Mock(
|
||||
hass=hass,
|
||||
available=True,
|
||||
authorized=True,
|
||||
allow_unreachable=False,
|
||||
allow_groups=False,
|
||||
api=Mock(),
|
||||
reset_jobs=[],
|
||||
spec=hue.HueBridge,
|
||||
)
|
||||
bridge.mock_requests = []
|
||||
@@ -218,7 +220,6 @@ def mock_bridge(hass):
|
||||
async def setup_bridge(hass, mock_bridge):
|
||||
"""Load the Hue light platform with the provided bridge."""
|
||||
hass.config.components.add(hue.DOMAIN)
|
||||
hass.data[hue.DOMAIN] = {"mock-host": mock_bridge}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1,
|
||||
hue.DOMAIN,
|
||||
@@ -228,6 +229,8 @@ async def setup_bridge(hass, mock_bridge):
|
||||
config_entries.CONN_CLASS_LOCAL_POLL,
|
||||
system_options={},
|
||||
)
|
||||
mock_bridge.config_entry = config_entry
|
||||
hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge}
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, "light")
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
@@ -363,8 +366,8 @@ async def test_new_group_discovered(hass, mock_bridge):
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": "light.group_1"}, blocking=True
|
||||
)
|
||||
# 2x group update, 2x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 5
|
||||
# 2x group update, 1x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 4
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
new_group = hass.states.get("light.group_3")
|
||||
@@ -443,8 +446,8 @@ async def test_group_removed(hass, mock_bridge):
|
||||
"light", "turn_on", {"entity_id": "light.group_1"}, blocking=True
|
||||
)
|
||||
|
||||
# 2x group update, 2x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 5
|
||||
# 2x group update, 1x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 4
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
group = hass.states.get("light.group_1")
|
||||
@@ -524,8 +527,8 @@ async def test_other_group_update(hass, mock_bridge):
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": "light.group_1"}, blocking=True
|
||||
)
|
||||
# 2x group update, 2x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 5
|
||||
# 2x group update, 1x light update, 1 turn on request
|
||||
assert len(mock_bridge.mock_requests) == 4
|
||||
assert len(hass.states.async_all()) == 2
|
||||
|
||||
group_2 = hass.states.get("light.group_2")
|
||||
@@ -599,7 +602,6 @@ async def test_update_timeout(hass, mock_bridge):
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
assert mock_bridge.available is False
|
||||
|
||||
|
||||
async def test_update_unauthorized(hass, mock_bridge):
|
||||
@@ -701,7 +703,7 @@ def test_available():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(allow_unreachable=False),
|
||||
is_group=False,
|
||||
)
|
||||
@@ -715,7 +717,7 @@ def test_available():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(allow_unreachable=True),
|
||||
is_group=False,
|
||||
)
|
||||
@@ -729,7 +731,7 @@ def test_available():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(allow_unreachable=False),
|
||||
is_group=True,
|
||||
)
|
||||
@@ -746,7 +748,7 @@ def test_hs_color():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(),
|
||||
is_group=False,
|
||||
)
|
||||
@@ -760,7 +762,7 @@ def test_hs_color():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(),
|
||||
is_group=False,
|
||||
)
|
||||
@@ -774,7 +776,7 @@ def test_hs_color():
|
||||
colorgamuttype=LIGHT_GAMUT_TYPE,
|
||||
colorgamut=LIGHT_GAMUT,
|
||||
),
|
||||
request_bridge_update=None,
|
||||
coordinator=Mock(failed_last_update=False),
|
||||
bridge=Mock(),
|
||||
is_group=False,
|
||||
)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Philips Hue sensors platform tests."""
|
||||
import asyncio
|
||||
from collections import deque
|
||||
import datetime
|
||||
import logging
|
||||
from unittest.mock import Mock
|
||||
|
||||
@@ -252,16 +251,19 @@ SENSOR_RESPONSE = {
|
||||
}
|
||||
|
||||
|
||||
def create_mock_bridge():
|
||||
def create_mock_bridge(hass):
|
||||
"""Create a mock Hue bridge."""
|
||||
bridge = Mock(
|
||||
hass=hass,
|
||||
available=True,
|
||||
authorized=True,
|
||||
allow_unreachable=False,
|
||||
allow_groups=False,
|
||||
api=Mock(),
|
||||
reset_jobs=[],
|
||||
spec=hue.HueBridge,
|
||||
)
|
||||
bridge.sensor_manager = hue_sensor_base.SensorManager(bridge)
|
||||
bridge.mock_requests = []
|
||||
# We're using a deque so we can schedule multiple responses
|
||||
# and also means that `popleft()` will blow up if we get more updates
|
||||
@@ -289,13 +291,7 @@ def create_mock_bridge():
|
||||
@pytest.fixture
|
||||
def mock_bridge(hass):
|
||||
"""Mock a Hue bridge."""
|
||||
return create_mock_bridge()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def increase_scan_interval(hass):
|
||||
"""Increase the SCAN_INTERVAL to prevent unexpected scans during tests."""
|
||||
hue_sensor_base.SensorManager.SCAN_INTERVAL = datetime.timedelta(days=365)
|
||||
return create_mock_bridge(hass)
|
||||
|
||||
|
||||
async def setup_bridge(hass, mock_bridge, hostname=None):
|
||||
@@ -303,7 +299,6 @@ async def setup_bridge(hass, mock_bridge, hostname=None):
|
||||
if hostname is None:
|
||||
hostname = "mock-host"
|
||||
hass.config.components.add(hue.DOMAIN)
|
||||
hass.data[hue.DOMAIN] = {hostname: mock_bridge}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1,
|
||||
hue.DOMAIN,
|
||||
@@ -313,6 +308,8 @@ async def setup_bridge(hass, mock_bridge, hostname=None):
|
||||
config_entries.CONN_CLASS_LOCAL_POLL,
|
||||
system_options={},
|
||||
)
|
||||
mock_bridge.config_entry = config_entry
|
||||
hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge}
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
|
||||
# and make sure it completes before going further
|
||||
@@ -330,7 +327,7 @@ async def test_no_sensors(hass, mock_bridge):
|
||||
|
||||
async def test_sensors_with_multiple_bridges(hass, mock_bridge):
|
||||
"""Test the update_items function with some sensors."""
|
||||
mock_bridge_2 = create_mock_bridge()
|
||||
mock_bridge_2 = create_mock_bridge(hass)
|
||||
mock_bridge_2.mock_sensor_responses.append(
|
||||
{
|
||||
"1": PRESENCE_SENSOR_3_PRESENT,
|
||||
@@ -412,11 +409,7 @@ async def test_new_sensor_discovered(hass, mock_bridge):
|
||||
mock_bridge.mock_sensor_responses.append(new_sensor_response)
|
||||
|
||||
# Force updates to run again
|
||||
sm_key = hue_sensor_base.SENSOR_MANAGER_FORMAT.format("mock-host")
|
||||
sm = hass.data[hue.DOMAIN][sm_key]
|
||||
await sm.async_update_items()
|
||||
|
||||
# To flush out the service call to update the group
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 2
|
||||
@@ -443,9 +436,7 @@ async def test_sensor_removed(hass, mock_bridge):
|
||||
mock_bridge.mock_sensor_responses.append({k: SENSOR_RESPONSE[k] for k in keys})
|
||||
|
||||
# Force updates to run again
|
||||
sm_key = hue_sensor_base.SENSOR_MANAGER_FORMAT.format("mock-host")
|
||||
sm = hass.data[hue.DOMAIN][sm_key]
|
||||
await sm.async_update_items()
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
@@ -466,7 +457,6 @@ async def test_update_timeout(hass, mock_bridge):
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
assert mock_bridge.available is False
|
||||
|
||||
|
||||
async def test_update_unauthorized(hass, mock_bridge):
|
||||
|
||||
Reference in new issue
Block a user