Fix Hue sensor integration with multiple bridges. (#23713)

pull/23746/head
Richard Mitchell 2019-05-07 18:45:04 +02:00 committed by Paulus Schoutsen
parent 02d8731a61
commit 482cb0146a
2 changed files with 37 additions and 11 deletions

View File

@ -13,7 +13,7 @@ from homeassistant.util.dt import utcnow
CURRENT_SENSORS = 'current_sensors'
SENSOR_MANAGER = 'sensor_manager'
SENSOR_MANAGER_FORMAT = '{}_sensor_manager'
_LOGGER = logging.getLogger(__name__)
@ -32,10 +32,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities,
bridge = hass.data[hue.DOMAIN][config_entry.data['host']]
hass.data[hue.DOMAIN].setdefault(CURRENT_SENSORS, {})
manager = hass.data[hue.DOMAIN].get(SENSOR_MANAGER)
sm_key = SENSOR_MANAGER_FORMAT.format(config_entry.data['host'])
manager = hass.data[hue.DOMAIN].get(sm_key)
if manager is None:
manager = SensorManager(hass, bridge)
hass.data[hue.DOMAIN][SENSOR_MANAGER] = manager
hass.data[hue.DOMAIN][sm_key] = manager
manager.register_component(binary, async_add_entities)
await manager.start()

View File

@ -321,9 +321,8 @@ SENSOR_RESPONSE = {
}
@pytest.fixture
def mock_bridge(hass):
"""Mock a Hue bridge."""
def create_mock_bridge():
"""Create a mock Hue bridge."""
bridge = Mock(
available=True,
allow_unreachable=False,
@ -348,22 +347,29 @@ def mock_bridge(hass):
bridge.api.config.apiversion = '9.9.9'
bridge.api.sensors = Sensors({}, mock_request)
return 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)
async def setup_bridge(hass, mock_bridge):
async def setup_bridge(hass, mock_bridge, hostname=None):
"""Load the Hue platform with the provided bridge."""
if hostname is None:
hostname = 'mock-host'
hass.config.components.add(hue.DOMAIN)
hass.data[hue.DOMAIN] = {'mock-host': mock_bridge}
hass.data[hue.DOMAIN] = {hostname: mock_bridge}
config_entry = config_entries.ConfigEntry(1, hue.DOMAIN, 'Mock Title', {
'host': 'mock-host'
'host': hostname
}, 'test', config_entries.CONN_CLASS_LOCAL_POLL)
await hass.config_entries.async_forward_entry_setup(
config_entry, 'binary_sensor')
@ -382,6 +388,24 @@ async def test_no_sensors(hass, mock_bridge):
assert len(hass.states.async_all()) == 0
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.mock_sensor_responses.append({
"1": PRESENCE_SENSOR_3_PRESENT,
"2": LIGHT_LEVEL_SENSOR_3,
"3": TEMPERATURE_SENSOR_3,
})
mock_bridge.mock_sensor_responses.append(SENSOR_RESPONSE)
await setup_bridge(hass, mock_bridge)
await setup_bridge(hass, mock_bridge_2, hostname='mock-bridge-2')
assert len(mock_bridge.mock_requests) == 1
assert len(mock_bridge_2.mock_requests) == 1
# 3 "physical" sensors with 3 virtual sensors each
assert len(hass.states.async_all()) == 9
async def test_sensors(hass, mock_bridge):
"""Test the update_items function with some sensors."""
mock_bridge.mock_sensor_responses.append(SENSOR_RESPONSE)
@ -450,7 +474,8 @@ async def test_new_sensor_discovered(hass, mock_bridge):
mock_bridge.mock_sensor_responses.append(new_sensor_response)
# Force updates to run again
sm = hass.data[hue.DOMAIN][hue_sensor_base.SENSOR_MANAGER]
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