Add support for multiple vera controller hubs (#33613)

This commit is contained in:
Robert Van Gorkom authored and GitHub committed 2020-09-14 22:06:52 -05:00
1 parent 938e06c00e
commit 903afb62d0
24 files changed
+323 -149

No files matched your search

+63 -15
View File
@@ -1,10 +1,15 @@
"""Common code for tests."""
from enum import Enum
from typing import Callable, Dict, NamedTuple, Tuple
import pyvera as pv
from homeassistant.components.vera.const import CONF_CONTROLLER, DOMAIN
from homeassistant import config_entries
from homeassistant.components.vera.const import (
CONF_CONTROLLER,
CONF_LEGACY_UNIQUE_ID,
DOMAIN,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@@ -24,7 +29,15 @@ class ControllerData(NamedTuple):
class ComponentData(NamedTuple):
"""Test data about the vera component."""
controller_data: ControllerData
controller_data: Tuple[ControllerData]
class ConfigSource(Enum):
"""Source of configuration."""
FILE = "file"
CONFIG_FLOW = "config_flow"
CONFIG_ENTRY = "config_entry"
class ControllerConfig(NamedTuple):
@@ -32,31 +45,34 @@ class ControllerConfig(NamedTuple):
config: Dict
options: Dict
config_from_file: bool
config_source: ConfigSource
serial_number: str
devices: Tuple[pv.VeraDevice, ...]
scenes: Tuple[pv.VeraScene, ...]
setup_callback: SetupCallback
legacy_entity_unique_id: bool
def new_simple_controller_config(
config: dict = None,
options: dict = None,
config_from_file=False,
config_source=ConfigSource.CONFIG_FLOW,
serial_number="1111",
devices: Tuple[pv.VeraDevice, ...] = (),
scenes: Tuple[pv.VeraScene, ...] = (),
setup_callback: SetupCallback = None,
legacy_entity_unique_id=False,
) -> ControllerConfig:
"""Create simple contorller config."""
return ControllerConfig(
config=config or {CONF_CONTROLLER: "http://127.0.0.1:123"},
options=options,
config_from_file=config_from_file,
config_source=config_source,
serial_number=serial_number,
devices=devices,
scenes=scenes,
setup_callback=setup_callback,
legacy_entity_unique_id=legacy_entity_unique_id,
)
@@ -68,14 +84,38 @@ class ComponentFactory:
self.vera_controller_class_mock = vera_controller_class_mock
async def configure_component(
self, hass: HomeAssistant, controller_config: ControllerConfig
self,
hass: HomeAssistant,
controller_config: ControllerConfig = None,
controller_configs: Tuple[ControllerConfig] = (),
) -> ComponentData:
"""Configure the component with multiple specific mock data."""
configs = list(controller_configs)
if controller_config:
configs.append(controller_config)
return ComponentData(
controller_data=tuple(
[
await self._configure_component(hass, controller_config)
for controller_config in configs
]
)
)
async def _configure_component(
self, hass: HomeAssistant, controller_config: ControllerConfig
) -> ControllerData:
"""Configure the component with specific mock data."""
component_config = {
**(controller_config.config or {}),
**(controller_config.options or {}),
}
if controller_config.legacy_entity_unique_id:
component_config[CONF_LEGACY_UNIQUE_ID] = True
controller = MagicMock(spec=pv.VeraController) # type: pv.VeraController
controller.base_url = component_config.get(CONF_CONTROLLER)
controller.register = MagicMock()
@@ -101,7 +141,7 @@ class ComponentFactory:
hass_config = {}
# Setup component through config file import.
if controller_config.config_from_file:
if controller_config.config_source == ConfigSource.FILE:
hass_config[DOMAIN] = component_config
# Setup Home Assistant.
@@ -109,9 +149,21 @@ class ComponentFactory:
await hass.async_block_till_done()
# Setup component through config flow.
if not controller_config.config_from_file:
if controller_config.config_source == ConfigSource.CONFIG_FLOW:
await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=component_config,
)
await hass.async_block_till_done()
# Setup component directly from config entry.
if controller_config.config_source == ConfigSource.CONFIG_ENTRY:
entry = MockConfigEntry(
domain=DOMAIN, data=component_config, options={}, unique_id="12345"
domain=DOMAIN,
data=controller_config.config,
options=controller_config.options,
unique_id="12345",
)
entry.add_to_hass(hass)
@@ -124,8 +176,4 @@ class ComponentFactory:
else None
)
return ComponentData(
controller_data=ControllerData(
controller=controller, update_callback=update_callback
)
)
return ControllerData(controller=controller, update_callback=update_callback)
+2 -2
View File
@@ -14,7 +14,7 @@ async def test_binary_sensor(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device.device_id = 1
vera_device.vera_device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.is_tripped = False
entity_id = "binary_sensor.dev1_1"
@@ -23,7 +23,7 @@ async def test_binary_sensor(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
vera_device.is_tripped = False
update_callback(vera_device)
+4 -2
View File
@@ -22,6 +22,7 @@ async def test_climate(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraThermostat) # type: pv.VeraThermostat
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_THERMOSTAT
vera_device.power = 10
@@ -34,7 +35,7 @@ async def test_climate(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == HVAC_MODE_OFF
@@ -131,6 +132,7 @@ async def test_climate_f(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraThermostat) # type: pv.VeraThermostat
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_THERMOSTAT
vera_device.power = 10
@@ -148,7 +150,7 @@ async def test_climate_f(
devices=(vera_device,), setup_callback=setup_callback
),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
await hass.services.async_call(
"climate",
+24 -27
View File
@@ -2,17 +2,13 @@
from requests.exceptions import RequestException
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.vera import CONF_CONTROLLER, DOMAIN
from homeassistant.components.vera import CONF_CONTROLLER, CONF_LEGACY_UNIQUE_ID, DOMAIN
from homeassistant.const import CONF_EXCLUDE, CONF_LIGHTS, CONF_SOURCE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
from tests.async_mock import MagicMock, patch
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, mock_registry
async def test_async_step_user_success(hass: HomeAssistant) -> None:
@@ -44,6 +40,7 @@ async def test_async_step_user_success(hass: HomeAssistant) -> None:
CONF_SOURCE: config_entries.SOURCE_USER,
CONF_LIGHTS: [12, 13],
CONF_EXCLUDE: [14, 15],
CONF_LEGACY_UNIQUE_ID: False,
}
assert result["result"].unique_id == controller.serial_number
@@ -51,18 +48,6 @@ async def test_async_step_user_success(hass: HomeAssistant) -> None:
assert entries
async def test_async_step_user_already_configured(hass: HomeAssistant) -> None:
"""Test user step with entry already configured."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options={}, unique_id="12345")
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_async_step_import_success(hass: HomeAssistant) -> None:
"""Test import step success."""
with patch("pyvera.VeraController") as vera_controller_class_mock:
@@ -82,28 +67,40 @@ async def test_async_step_import_success(hass: HomeAssistant) -> None:
assert result["data"] == {
CONF_CONTROLLER: "http://127.0.0.1:123",
CONF_SOURCE: config_entries.SOURCE_IMPORT,
CONF_LEGACY_UNIQUE_ID: False,
}
assert result["result"].unique_id == controller.serial_number
async def test_async_step_import_alredy_setup(hass: HomeAssistant) -> None:
"""Test import step with entry already setup."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options={}, unique_id="12345")
entry.add_to_hass(hass)
async def test_async_step_import_success_with_legacy_unique_id(
hass: HomeAssistant,
) -> None:
"""Test import step success with legacy unique id."""
entity_registry = mock_registry(hass)
entity_registry.async_get_or_create(
domain="switch", platform=DOMAIN, unique_id="12"
)
with patch("pyvera.VeraController") as vera_controller_class_mock:
controller = MagicMock()
controller.refresh_data = MagicMock()
controller.serial_number = "12345"
controller.serial_number = "serial_number_1"
vera_controller_class_mock.return_value = controller
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={CONF_CONTROLLER: "http://localhost:445"},
data={CONF_CONTROLLER: "http://127.0.0.1:123/"},
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "http://127.0.0.1:123"
assert result["data"] == {
CONF_CONTROLLER: "http://127.0.0.1:123",
CONF_SOURCE: config_entries.SOURCE_IMPORT,
CONF_LEGACY_UNIQUE_ID: True,
}
assert result["result"].unique_id == controller.serial_number
async def test_async_step_finish_error(hass: HomeAssistant) -> None:
+2 -1
View File
@@ -14,6 +14,7 @@ async def test_cover(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraCurtain) # type: pv.VeraCurtain
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_CURTAIN
vera_device.is_closed = False
@@ -24,7 +25,7 @@ async def test_cover(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == "closed"
assert hass.states.get(entity_id).attributes["current_position"] == 0
+70 -13
View File
@@ -12,10 +12,10 @@ from homeassistant.components.vera import (
from homeassistant.config_entries import ENTRY_STATE_NOT_LOADED
from homeassistant.core import HomeAssistant
from .common import ComponentFactory, new_simple_controller_config
from .common import ComponentFactory, ConfigSource, new_simple_controller_config
from tests.async_mock import MagicMock
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, mock_registry
async def test_init(
@@ -24,7 +24,7 @@ async def test_init(
"""Test function."""
vera_device1 = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device1.device_id = 1
vera_device1.vera_device_id = 1
vera_device1.vera_device_id = vera_device1.device_id
vera_device1.name = "first_dev"
vera_device1.is_tripped = False
entity1_id = "binary_sensor.first_dev_1"
@@ -33,7 +33,7 @@ async def test_init(
hass=hass,
controller_config=new_simple_controller_config(
config={CONF_CONTROLLER: "http://127.0.0.1:111"},
config_from_file=False,
config_source=ConfigSource.CONFIG_FLOW,
serial_number="first_serial",
devices=(vera_device1,),
),
@@ -41,8 +41,8 @@ async def test_init(
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry1 = entity_registry.async_get(entity1_id)
assert entry1
assert entry1.unique_id == "vera_first_serial_1"
async def test_init_from_file(
@@ -51,7 +51,7 @@ async def test_init_from_file(
"""Test function."""
vera_device1 = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device1.device_id = 1
vera_device1.vera_device_id = 1
vera_device1.vera_device_id = vera_device1.device_id
vera_device1.name = "first_dev"
vera_device1.is_tripped = False
entity1_id = "binary_sensor.first_dev_1"
@@ -60,7 +60,7 @@ async def test_init_from_file(
hass=hass,
controller_config=new_simple_controller_config(
config={CONF_CONTROLLER: "http://127.0.0.1:111"},
config_from_file=True,
config_source=ConfigSource.FILE,
serial_number="first_serial",
devices=(vera_device1,),
),
@@ -69,6 +69,62 @@ async def test_init_from_file(
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry1 = entity_registry.async_get(entity1_id)
assert entry1
assert entry1.unique_id == "vera_first_serial_1"
async def test_multiple_controllers_with_legacy_one(
hass: HomeAssistant, vera_component_factory: ComponentFactory
) -> None:
"""Test multiple controllers with one legacy controller."""
vera_device1 = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device1.device_id = 1
vera_device1.vera_device_id = vera_device1.device_id
vera_device1.name = "first_dev"
vera_device1.is_tripped = False
entity1_id = "binary_sensor.first_dev_1"
vera_device2 = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device2.device_id = 2
vera_device2.vera_device_id = vera_device2.device_id
vera_device2.name = "second_dev"
vera_device2.is_tripped = False
entity2_id = "binary_sensor.second_dev_2"
# Add existing entity registry entry from previous setup.
entity_registry = mock_registry(hass)
entity_registry.async_get_or_create(
domain="switch", platform=DOMAIN, unique_id="12"
)
await vera_component_factory.configure_component(
hass=hass,
controller_config=new_simple_controller_config(
config={CONF_CONTROLLER: "http://127.0.0.1:111"},
config_source=ConfigSource.FILE,
serial_number="first_serial",
devices=(vera_device1,),
),
)
await vera_component_factory.configure_component(
hass=hass,
controller_config=new_simple_controller_config(
config={CONF_CONTROLLER: "http://127.0.0.1:222"},
config_source=ConfigSource.CONFIG_FLOW,
serial_number="second_serial",
devices=(vera_device2,),
),
)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry1 = entity_registry.async_get(entity1_id)
assert entry1
assert entry1.unique_id == "1"
entry2 = entity_registry.async_get(entity2_id)
assert entry2
assert entry2.unique_id == "vera_second_serial_2"
async def test_unload(
@@ -77,7 +133,7 @@ async def test_unload(
"""Test function."""
vera_device1 = MagicMock(spec=pv.VeraBinarySensor) # type: pv.VeraBinarySensor
vera_device1.device_id = 1
vera_device1.vera_device_id = 1
vera_device1.vera_device_id = vera_device1.device_id
vera_device1.name = "first_dev"
vera_device1.is_tripped = False
@@ -145,6 +201,7 @@ async def test_exclude_and_light_ids(
vera_device3 = MagicMock(spec=pv.VeraSwitch) # type: pv.VeraSwitch
vera_device3.device_id = 3
vera_device3.vera_device_id = 3
vera_device3.name = "dev3"
vera_device3.category = pv.CATEGORY_SWITCH
vera_device3.is_switched_on = MagicMock(return_value=False)
@@ -152,6 +209,7 @@ async def test_exclude_and_light_ids(
vera_device4 = MagicMock(spec=pv.VeraSwitch) # type: pv.VeraSwitch
vera_device4.device_id = 4
vera_device4.vera_device_id = 4
vera_device4.name = "dev4"
vera_device4.category = pv.CATEGORY_SWITCH
vera_device4.is_switched_on = MagicMock(return_value=False)
@@ -160,6 +218,7 @@ async def test_exclude_and_light_ids(
component_data = await vera_component_factory.configure_component(
hass=hass,
controller_config=new_simple_controller_config(
config_source=ConfigSource.CONFIG_ENTRY,
devices=(vera_device1, vera_device2, vera_device3, vera_device4),
config={**{CONF_CONTROLLER: "http://127.0.0.1:123"}, **options},
),
@@ -167,12 +226,10 @@ async def test_exclude_and_light_ids(
# Assert the entries were setup correctly.
config_entry = next(iter(hass.config_entries.async_entries(DOMAIN)))
assert config_entry.options == {
CONF_LIGHTS: [4, 10, 12],
CONF_EXCLUDE: [1],
}
assert config_entry.options[CONF_LIGHTS] == [4, 10, 12]
assert config_entry.options[CONF_EXCLUDE] == [1]
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
update_callback(vera_device1)
update_callback(vera_device2)
+2 -1
View File
@@ -15,6 +15,7 @@ async def test_light(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraDimmer) # type: pv.VeraDimmer
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_DIMMER
vera_device.is_switched_on = MagicMock(return_value=False)
@@ -27,7 +28,7 @@ async def test_light(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == "off"
+2 -1
View File
@@ -15,6 +15,7 @@ async def test_lock(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraLock) # type: pv.VeraLock
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_LOCK
vera_device.is_locked = MagicMock(return_value=False)
@@ -24,7 +25,7 @@ async def test_lock(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == STATE_UNLOCKED
+1
View File
@@ -14,6 +14,7 @@ async def test_scene(
"""Test function."""
vera_scene = MagicMock(spec=pv.VeraScene) # type: pv.VeraScene
vera_scene.scene_id = 1
vera_scene.vera_scene_id = vera_scene.scene_id
vera_scene.name = "dev1"
entity_id = "scene.dev1_1"
+4 -2
View File
@@ -23,6 +23,7 @@ async def run_sensor_test(
"""Test generic sensor."""
vera_device = MagicMock(spec=pv.VeraSensor) # type: pv.VeraSensor
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = category
setattr(vera_device, class_property, "33")
@@ -34,7 +35,7 @@ async def run_sensor_test(
devices=(vera_device,), setup_callback=setup_callback
),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
for (initial_value, state_value) in assert_states:
setattr(vera_device, class_property, initial_value)
@@ -175,6 +176,7 @@ async def test_scene_controller_sensor(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraSensor) # type: pv.VeraSensor
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_SCENE_CONTROLLER
vera_device.get_last_scene_id = MagicMock(return_value="id0")
@@ -185,7 +187,7 @@ async def test_scene_controller_sensor(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
vera_device.get_last_scene_time.return_value = "1111"
update_callback(vera_device)
+5 -2
View File
@@ -14,6 +14,7 @@ async def test_switch(
"""Test function."""
vera_device = MagicMock(spec=pv.VeraSwitch) # type: pv.VeraSwitch
vera_device.device_id = 1
vera_device.vera_device_id = vera_device.device_id
vera_device.name = "dev1"
vera_device.category = pv.CATEGORY_SWITCH
vera_device.is_switched_on = MagicMock(return_value=False)
@@ -21,9 +22,11 @@ async def test_switch(
component_data = await vera_component_factory.configure_component(
hass=hass,
controller_config=new_simple_controller_config(devices=(vera_device,)),
controller_config=new_simple_controller_config(
devices=(vera_device,), legacy_entity_unique_id=False
),
)
update_callback = component_data.controller_data.update_callback
update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == "off"