Add vera config entries support (#29880)

* Adding vera config entries support.

* Fixing lint error.

* Applying minimal changes necessary to get config entries working.

* Addressing PR feedback by further reducing the scope of the change.

* Addressing PR feedback.

* Fixing pyvera import to make it easier to patch.
Addressing PR feedback regarding creation of controller and scheduling of async config flow actions.

* Updating code owners file.

* Small fixes.

* Adding a user config flow step.

* Adding optional configs for user config flow.

* Updating strings to be more clear to the user.

* Adding options flow.
Fixing some PR feedback.

* Better handling of options.
PR feedback changes.

* Using config registry to update config options.

* Better managing config from file or config from UI
Disabling config through UI if config is provided from a file.
More tests to account for these adjustments.

* Address PR feedback.

* Fixing test, merging with master.

* Disabling all Vera UI for configs managed by configuration.yml.
Adding more tests.

* Updating config based on unique_id.
Addressing additional PR feedback.

* Rebasing off dev.
Addressing feedback.

* Addressing PR feedback.
This commit is contained in:
Robert Van Gorkom authored and GitHub committed 2020-04-03 09:49:50 +02:00
1 parent aef06a3544
commit ae22b5187a
28 files changed
+876 -294

No files matched your search

+94 -28
View File
@@ -1,47 +1,91 @@
"""Common code for tests."""
from typing import Callable, NamedTuple, Tuple
from typing import Callable, Dict, NamedTuple, Tuple
from mock import MagicMock
from pyvera import VeraController, VeraDevice, VeraScene
import pyvera as pv
from homeassistant.components.vera import CONF_CONTROLLER, DOMAIN
from homeassistant.components.vera.const import CONF_CONTROLLER, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
SetupCallback = Callable[[pv.VeraController, dict], None]
class ControllerData(NamedTuple):
"""Test data about a specific vera controller."""
controller: pv.VeraController
update_callback: Callable
class ComponentData(NamedTuple):
"""Component data."""
"""Test data about the vera component."""
controller: VeraController
controller_data: ControllerData
class ControllerConfig(NamedTuple):
"""Test config for mocking a vera controller."""
config: Dict
options: Dict
config_from_file: bool
serial_number: str
devices: Tuple[pv.VeraDevice, ...]
scenes: Tuple[pv.VeraScene, ...]
setup_callback: SetupCallback
def new_simple_controller_config(
config: dict = None,
options: dict = None,
config_from_file=False,
serial_number="1111",
devices: Tuple[pv.VeraDevice, ...] = (),
scenes: Tuple[pv.VeraScene, ...] = (),
setup_callback: SetupCallback = None,
) -> 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,
serial_number=serial_number,
devices=devices,
scenes=scenes,
setup_callback=setup_callback,
)
class ComponentFactory:
"""Factory class."""
def __init__(self, init_controller_mock):
"""Initialize component factory."""
self.init_controller_mock = init_controller_mock
def __init__(self, vera_controller_class_mock):
"""Initialize the factory."""
self.vera_controller_class_mock = vera_controller_class_mock
async def configure_component(
self,
hass: HomeAssistant,
devices: Tuple[VeraDevice] = (),
scenes: Tuple[VeraScene] = (),
setup_callback: Callable[[VeraController], None] = None,
self, hass: HomeAssistant, controller_config: ControllerConfig
) -> ComponentData:
"""Configure the component with specific mock data."""
controller_url = "http://127.0.0.1:123"
hass_config = {
DOMAIN: {CONF_CONTROLLER: controller_url},
component_config = {
**(controller_config.config or {}),
**(controller_config.options or {}),
}
controller = MagicMock(spec=VeraController) # type: VeraController
controller.base_url = controller_url
controller = MagicMock(spec=pv.VeraController) # type: pv.VeraController
controller.base_url = component_config.get(CONF_CONTROLLER)
controller.register = MagicMock()
controller.get_devices = MagicMock(return_value=devices or ())
controller.get_scenes = MagicMock(return_value=scenes or ())
controller.start = MagicMock()
controller.stop = MagicMock()
controller.refresh_data = MagicMock()
controller.temperature_units = "C"
controller.serial_number = controller_config.serial_number
controller.get_devices = MagicMock(return_value=controller_config.devices)
controller.get_scenes = MagicMock(return_value=controller_config.scenes)
for vera_obj in controller.get_devices() + controller.get_scenes():
vera_obj.vera_controller = controller
@@ -49,17 +93,39 @@ class ComponentFactory:
controller.get_devices.reset_mock()
controller.get_scenes.reset_mock()
if setup_callback:
setup_callback(controller, hass_config)
if controller_config.setup_callback:
controller_config.setup_callback(controller)
def init_controller(base_url: str) -> list:
nonlocal controller
return [controller, True]
self.vera_controller_class_mock.return_value = controller
self.init_controller_mock.side_effect = init_controller
hass_config = {}
# Setup component through config file import.
if controller_config.config_from_file:
hass_config[DOMAIN] = component_config
# Setup Home Assistant.
assert await async_setup_component(hass, DOMAIN, hass_config)
await hass.async_block_till_done()
return ComponentData(controller=controller)
# Setup component through config flow.
if not controller_config.config_from_file:
entry = MockConfigEntry(
domain=DOMAIN, data=component_config, options={}, unique_id="12345"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
update_callback = (
controller.register.call_args_list[0][0][1]
if controller.register.call_args_list
else None
)
return ComponentData(
controller_data=ControllerData(
controller=controller, update_callback=update_callback
)
)