hass-core/tests/components/somfy/test_config_flow.py
tetienne 0a7919a279 Somfy open api (#19548)
* CREATE Somfy component

* CREATE cover Somfy platform

* USE somfy id as unique id

* UPDATE all the devices in one call to limit the number of call

* FIX Don't load devices if not yet configured

* IMP Replace configurator by a simple notification

* ADD log in case state does not match

* IMP wording

* REMOVE debug stuf

* ADD support for tilt position

* UPDATE requirements

* FIX Use code instead of authorization response

 - Will allow to setup Somfy without https

* HANDLE stateless devices (Somfy RTS)

* FIX import locally 3rd party library

* UPDATE pymfy to 0.4.3

* ADD missing docstring

* FIX For Somfy 100 means closed and 0 opened

* FIX position can be None

* ENHANCE error management when error 500 occurs at setup

* FIX indent

* ROLLBACK tilt modification

 - See https://community.home-assistant.io/t/somfy-tahoma-official-api/61448/90?u=tetienne

* FIX Look for capability instead of state

* DON'T use exception to test if a feature is available

* UPDATE dependency

* ADD device_info property

* AVOID object creation in each method

* REMOVE unused constants

* ADD missing doc

* IMP Only make one call to add_entities

* USE dict[key] instead of get method

* IMP Don't pass hass object to the entities

* FIX Don't end logging messages with period

* USE config entries instead of a cache file

* IMPLEMENT async_unload_entry

* CONSOLIDATE package

 - see home-assistant/architecture#124

* UPDATE to pymfy 0.5.1

* SIMPLIFY config flow

* ADD French translation

* FIX 80 vs 79 max length

* ABORT flow asap

* FIX A tupple was returned

* MIGRATE to manifest.json

* ADD a placeholder async_setup_platform coroutine

 - It's currently required and expected by the platform helper.

* FIX codeowner

* ADD missing translations file

* USE new external step

* UPGRADE pymfy version

* Close Somfy tab automatically

* ADD manufacturer

  - Somfy only for the moment.

* HANDLE missing code or state in Somfy request

* REMOVE unused strings

* DECLARE somfy component to use config_flow

* APPLY static check remarks

* FIX async method cannot be called from sync context

* FIX only unload what has been loaded during entry setup

* DON't catch them all

* DON'T log full stacktrace

* ABORT conflig flow if configuration missing

* OMIT Somfy files for coverage

* ADD tests about Somfy config flow

* ADD pymfy to the test dependencies
2019-06-11 08:45:34 -07:00

77 lines
2.9 KiB
Python

"""Tests for the Somfy config flow."""
import asyncio
from unittest.mock import Mock, patch
from pymfy.api.somfy_api import SomfyApi
from homeassistant import data_entry_flow
from homeassistant.components.somfy import config_flow, DOMAIN
from homeassistant.components.somfy.config_flow import \
register_flow_implementation
from tests.common import MockConfigEntry, mock_coro
CLIENT_SECRET_VALUE = "5678"
CLIENT_ID_VALUE = "1234"
AUTH_URL = 'http://somfy.com'
async def test_abort_if_no_configuration(hass):
"""Check flow abort when no configuration."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'missing_configuration'
async def test_abort_if_existing_entry(hass):
"""Check flow abort when an entry already exist."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
result = await flow.async_step_import()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
async def test_full_flow(hass):
"""Check classic use case."""
hass.data[DOMAIN] = {}
register_flow_implementation(hass, CLIENT_ID_VALUE, CLIENT_SECRET_VALUE)
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
hass.config.api = Mock(base_url='https://example.com')
flow._get_authorization_url = Mock(
return_value=mock_coro((AUTH_URL, 'state')))
result = await flow.async_step_import()
assert result['type'] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
assert result['url'] == AUTH_URL
result = await flow.async_step_auth("my_super_code")
assert result['type'] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP_DONE
assert result['step_id'] == 'creation'
assert flow.code == 'my_super_code'
with patch.object(SomfyApi, 'request_token',
return_value={"access_token": "super_token"}):
result = await flow.async_step_creation()
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['data']['refresh_args'] == {
'client_id': CLIENT_ID_VALUE,
'client_secret': CLIENT_SECRET_VALUE
}
assert result['title'] == 'Somfy'
assert result['data']['token'] == {"access_token": "super_token"}
async def test_abort_if_authorization_timeout(hass):
"""Check Somfy authorization timeout."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
flow._get_authorization_url = Mock(side_effect=asyncio.TimeoutError)
result = await flow.async_step_auth()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'authorize_url_timeout'