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
This commit is contained in:
Chris Talkington 2020-09-20 17:38:02 -05:00 committed by GitHub
parent 83b0954e58
commit 432911c994
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 18 deletions

View File

@ -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

View File

@ -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
}
),
)

View File

@ -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"