From 432911c994166614905f6b15f3caecdfa451f71f Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Sun, 20 Sep 2020 17:38:02 -0500 Subject: [PATCH] Apply code review for canary config flow (#40355) * apply code review for canary config flow * Update __init__.py * Update __init__.py * Update __init__.py * Update camera.py * Update camera.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update __init__.py --- homeassistant/components/canary/__init__.py | 30 +++++++++++++-------- homeassistant/components/canary/camera.py | 11 ++++++-- tests/components/canary/test_config_flow.py | 16 +++++++---- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/canary/__init__.py b/homeassistant/components/canary/__init__.py index c0245b5b9d0..06f4134e24e 100644 --- a/homeassistant/components/canary/__init__.py +++ b/homeassistant/components/canary/__init__.py @@ -89,10 +89,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool hass.config_entries.async_update_entry(entry, options=options) try: - canary_data = CanaryData( - entry.data[CONF_USERNAME], - entry.data[CONF_PASSWORD], - entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT), + canary_data = await hass.async_add_executor_job( + _get_canary_data_instance, entry ) except (ConnectTimeout, HTTPError) as error: _LOGGER.error("Unable to connect to Canary service: %s", str(error)) @@ -137,18 +135,14 @@ async def _async_update_listener(hass: HomeAssistantType, entry: ConfigEntry) -> class CanaryData: - """Get the latest data and update the states.""" + """Manages the data retrieved from Canary API.""" - def __init__(self, username, password, timeout): + def __init__(self, api: Api): """Init the Canary data object.""" - - self._api = Api(username, password, timeout) - + self._api = api self._locations_by_id = {} self._readings_by_device_id = {} - self.update() - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self, **kwargs): """Get the latest data from py-canary with a throttle.""" @@ -200,3 +194,17 @@ class CanaryData: def get_live_stream_session(self, device): """Return live stream session.""" return self._api.get_live_stream_session(device) + + +def _get_canary_data_instance(entry: ConfigEntry) -> CanaryData: + """Initialize a new instance of CanaryData.""" + canary = Api( + entry.data[CONF_USERNAME], + entry.data[CONF_PASSWORD], + entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT), + ) + + canary_data = CanaryData(canary) + canary_data.update() + + return canary_data diff --git a/homeassistant/components/canary/camera.py b/homeassistant/components/canary/camera.py index bbf1284c602..1cc7a535344 100644 --- a/homeassistant/components/canary/camera.py +++ b/homeassistant/components/canary/camera.py @@ -31,8 +31,15 @@ _LOGGER = logging.getLogger(__name__) MIN_TIME_BETWEEN_SESSION_RENEW = timedelta(seconds=90) -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - {vol.Optional(CONF_FFMPEG_ARGUMENTS, default=DEFAULT_FFMPEG_ARGUMENTS): cv.string} +PLATFORM_SCHEMA = vol.All( + cv.deprecated(CONF_FFMPEG_ARGUMENTS, invalidation_version="0.118"), + PLATFORM_SCHEMA.extend( + { + vol.Optional( + CONF_FFMPEG_ARGUMENTS, default=DEFAULT_FFMPEG_ARGUMENTS + ): cv.string + } + ), ) diff --git a/tests/components/canary/test_config_flow.py b/tests/components/canary/test_config_flow.py index 2bd6ae6443d..36c6990a663 100644 --- a/tests/components/canary/test_config_flow.py +++ b/tests/components/canary/test_config_flow.py @@ -18,6 +18,8 @@ from homeassistant.setup import async_setup_component from . import USER_INPUT, _patch_async_setup, _patch_async_setup_entry, init_integration +from tests.async_mock import patch + async def test_user_form(hass, canary_config_flow): """Test we get the user initiated form.""" @@ -103,7 +105,9 @@ async def test_user_form_single_instance_allowed(hass, canary_config_flow): async def test_options_flow(hass): """Test updating options.""" - entry = await init_integration(hass, skip_entry_setup=True) + with patch("homeassistant.components.canary.PLATFORMS", []): + entry = await init_integration(hass) + assert entry.options[CONF_FFMPEG_ARGUMENTS] == DEFAULT_FFMPEG_ARGUMENTS assert entry.options[CONF_TIMEOUT] == DEFAULT_TIMEOUT @@ -111,10 +115,12 @@ async def test_options_flow(hass): assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "init" - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={CONF_FFMPEG_ARGUMENTS: "-v", CONF_TIMEOUT: 7}, - ) + with _patch_async_setup(), _patch_async_setup_entry(): + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={CONF_FFMPEG_ARGUMENTS: "-v", CONF_TIMEOUT: 7}, + ) + await hass.async_block_till_done() assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["data"][CONF_FFMPEG_ARGUMENTS] == "-v"