hass-core/tests/components/ecobee/test_config_flow.py
Mark Coombes f6995b8d17 Add config flow to ecobee (#26634)
* Add basic config flow

* Fix json files

* Update __init__.py

* Fix json errors

* Move constants to const.py

* Add ecobee to generated config flows

* Update config_flow for updated API

* Update manifest to include new dependencies

Bump pyecobee, add aiofiles.

* Update constants for ecobee

* Modify ecobee setup to use config flow

* Bump dependency

* Update binary_sensor to use config_entry

* Update sensor to use config_entry

* Update __init__.py

* Update weather to use config_entry

* Update notify.py

* Update ecobee constants

* Update climate to use config_entry

* Avoid a breaking change on ecobee services

* Store api key from old config entry

* Allow unloading of config entry

* Show user a form before import

* Refine import flow

* Update strings.json to remove import step

Not needed.

* Move third party imports to top of module

* Remove periods from end of log messages

* Make configuration.yaml config optional

* Remove unused strings

* Reorganize config flow

* Remove unneeded requirement

* No need to store API key

* Update async_unload_entry

* Clean up if/else statements

* Update requirements_all.txt

* Fix config schema

* Update __init__.py

* Remove check for DATA_ECOBEE_CONFIG

* Remove redundant check

* Add check for DATA_ECOBEE_CONFIG

* Change setup_platform to async

* Fix state unknown and imports

* Change init step to user

* Have import step raise specific exceptions

* Rearrange try/except block in import flow

* Convert update() and refresh() to coroutines

...and update platforms to use async_update coroutine.

* Finish converting init to async

* Preliminary tests

* Test full implementation

* Update test_config_flow.py

* Update test_config_flow.py

* Add self to codeowners

* Update test_config_flow.py

* Use MockConfigEntry

* Update test_config_flow.py

* Update CODEOWNERS

* pylint fixes

* Register services under ecobee domain

Breaking change!

* Pylint fixes

* Pylint fixes

* Pylint fixes

* Move service strings to ecobee domain

* Fix log message capitalization

* Fix import formatting

* Update .coveragerc

* Add __init__ to coveragerc

* Add option flow test

* Update .coveragerc

* Act on updated options

* Revert "Act on updated options"

This reverts commit 56b0a859f2e3e80b6f4c77a8f784a2b29ee2cce9.

* Remove hold_temp from climate

* Remove hold_temp and options from init

* Remove options handler from config flow

* Remove options strings

* Remove options flow test

* Remove hold_temp constants

* Fix climate tests

* Pass api key to user step in import flow

* Update test_config_flow.py

Ensure that the import step calls the user step with the user's api key as user input if importing from ecobee.conf/validating imported keys fails.
2019-09-25 22:38:21 +02:00

206 lines
7.2 KiB
Python

"""Tests for the ecobee config flow."""
from unittest.mock import patch
from pyecobee import ECOBEE_API_KEY, ECOBEE_REFRESH_TOKEN
from homeassistant import data_entry_flow
from homeassistant.components.ecobee import config_flow
from homeassistant.components.ecobee.const import (
CONF_REFRESH_TOKEN,
DATA_ECOBEE_CONFIG,
DOMAIN,
)
from homeassistant.const import CONF_API_KEY
from tests.common import MockConfigEntry, mock_coro
async def test_abort_if_already_setup(hass):
"""Test we abort if ecobee is already setup."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
result = await flow.async_step_user()
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "one_instance_only"
async def test_user_step_without_user_input(hass):
"""Test expected result if user step is called."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
result = await flow.async_step_user()
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
async def test_pin_request_succeeds(hass):
"""Test expected result if pin request succeeds."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
with patch("homeassistant.components.ecobee.config_flow.Ecobee") as MockEcobee:
mock_ecobee = MockEcobee.return_value
mock_ecobee.request_pin.return_value = True
mock_ecobee.pin = "test-pin"
result = await flow.async_step_user(user_input={CONF_API_KEY: "api-key"})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "authorize"
assert result["description_placeholders"] == {"pin": "test-pin"}
async def test_pin_request_fails(hass):
"""Test expected result if pin request fails."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
with patch("homeassistant.components.ecobee.config_flow.Ecobee") as MockEcobee:
mock_ecobee = MockEcobee.return_value
mock_ecobee.request_pin.return_value = False
result = await flow.async_step_user(user_input={CONF_API_KEY: "api-key"})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"]["base"] == "pin_request_failed"
async def test_token_request_succeeds(hass):
"""Test expected result if token request succeeds."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
with patch("homeassistant.components.ecobee.config_flow.Ecobee") as MockEcobee:
mock_ecobee = MockEcobee.return_value
mock_ecobee.request_tokens.return_value = True
mock_ecobee.api_key = "test-api-key"
mock_ecobee.refresh_token = "test-token"
flow._ecobee = mock_ecobee
result = await flow.async_step_authorize(user_input={})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == DOMAIN
assert result["data"] == {
CONF_API_KEY: "test-api-key",
CONF_REFRESH_TOKEN: "test-token",
}
async def test_token_request_fails(hass):
"""Test expected result if token request fails."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
with patch("homeassistant.components.ecobee.config_flow.Ecobee") as MockEcobee:
mock_ecobee = MockEcobee.return_value
mock_ecobee.request_tokens.return_value = False
mock_ecobee.pin = "test-pin"
flow._ecobee = mock_ecobee
result = await flow.async_step_authorize(user_input={})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "authorize"
assert result["errors"]["base"] == "token_request_failed"
assert result["description_placeholders"] == {"pin": "test-pin"}
async def test_import_flow_triggered_but_no_ecobee_conf(hass):
"""Test expected result if import flow triggers but ecobee.conf doesn't exist."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {}
result = await flow.async_step_import(import_data=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
async def test_import_flow_triggered_with_ecobee_conf_and_valid_data_and_valid_tokens(
hass
):
"""Test expected result if import flow triggers and ecobee.conf exists with valid tokens."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
MOCK_ECOBEE_CONF = {ECOBEE_API_KEY: None, ECOBEE_REFRESH_TOKEN: None}
with patch(
"homeassistant.components.ecobee.config_flow.load_json",
return_value=MOCK_ECOBEE_CONF,
), patch("homeassistant.components.ecobee.config_flow.Ecobee") as MockEcobee:
mock_ecobee = MockEcobee.return_value
mock_ecobee.refresh_tokens.return_value = True
mock_ecobee.api_key = "test-api-key"
mock_ecobee.refresh_token = "test-token"
result = await flow.async_step_import(import_data=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == DOMAIN
assert result["data"] == {
CONF_API_KEY: "test-api-key",
CONF_REFRESH_TOKEN: "test-token",
}
async def test_import_flow_triggered_with_ecobee_conf_and_invalid_data(hass):
"""Test expected result if import flow triggers and ecobee.conf exists with invalid data."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {CONF_API_KEY: "test-api-key"}
MOCK_ECOBEE_CONF = {}
with patch(
"homeassistant.components.ecobee.config_flow.load_json",
return_value=MOCK_ECOBEE_CONF,
), patch.object(
flow, "async_step_user", return_value=mock_coro()
) as mock_async_step_user:
await flow.async_step_import(import_data=None)
mock_async_step_user.assert_called_once_with(
user_input={CONF_API_KEY: "test-api-key"}
)
async def test_import_flow_triggered_with_ecobee_conf_and_valid_data_and_stale_tokens(
hass
):
"""Test expected result if import flow triggers and ecobee.conf exists with stale tokens."""
flow = config_flow.EcobeeFlowHandler()
flow.hass = hass
flow.hass.data[DATA_ECOBEE_CONFIG] = {CONF_API_KEY: "test-api-key"}
MOCK_ECOBEE_CONF = {ECOBEE_API_KEY: None, ECOBEE_REFRESH_TOKEN: None}
with patch(
"homeassistant.components.ecobee.config_flow.load_json",
return_value=MOCK_ECOBEE_CONF,
), patch(
"homeassistant.components.ecobee.config_flow.Ecobee"
) as MockEcobee, patch.object(
flow, "async_step_user", return_value=mock_coro()
) as mock_async_step_user:
mock_ecobee = MockEcobee.return_value
mock_ecobee.refresh_tokens.return_value = False
await flow.async_step_import(import_data=None)
mock_async_step_user.assert_called_once_with(
user_input={CONF_API_KEY: "test-api-key"}
)