Rewrite of Toon component (#21186)
* 🚜 Rewrite of Toon component * 🔥 Removed manual state from list * 👕 Addresses code review comments * 🔥 Removes a log line that should not have been left behind * 👕 Addresses linting warnings * 👕 Addresses Hound CI warning * 👕 Fixes small code styling issues * ✨ Sets an appropriate SCAN_INTERVAL * ✨ Sets min/max temperature for climate platform * 👕 Makes imports more consistent with codebase * 🚑 Fixes incorrect SCAN_INTERVAL value in climate platform * 🚑 Uses OrderedDict for config_flow schema * 👕 Adds return types for min/max temp * 🚜 Refactors entities into their actual devices * ⬆️ Updates toonapilib to 3.0.7 * 🚜 Refactors binary sensor state inversion * 🚑 Fixes states of OpenTherm connection and Hot Tap Water * ✨ Adds Boiler Preheat binary sensor * ✨ Adds Toon Thermostat Program binary sensor * ✨ Adds Boiler Modulation Level sensor * ✨ Adds Daily Power Cost sensor * 🔥 Cleanup of Toon Thermostat climate attributes * 🚜 Adjusts config_flow with Tenant selection * 🙋 Adds myself to codeowners file as maintainer * ⬆️ Gen requirements * ⬆️ Updates toonapilib to 3.0.9 * ☔ Adds config_flow tests
This commit is contained in:
1 parent
f0268688be
commit
f3c9327ccf
16 files changed
+943
-389
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Toon component."""
|
||||
@@ -0,0 +1,177 @@
|
||||
"""Tests for the Toon config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from toonapilib.toonapilibexceptions import (
|
||||
AgreementsRetrievalError, InvalidConsumerKey, InvalidConsumerSecret,
|
||||
InvalidCredentials)
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.toon import config_flow
|
||||
from homeassistant.components.toon.const import (
|
||||
CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_DISPLAY, CONF_TENANT, DOMAIN)
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, MockDependency
|
||||
|
||||
FIXTURE_APP = {
|
||||
DOMAIN: {
|
||||
CONF_CLIENT_ID: '1234567890abcdef',
|
||||
CONF_CLIENT_SECRET: '1234567890abcdef',
|
||||
}
|
||||
}
|
||||
|
||||
FIXTURE_CREDENTIALS = {
|
||||
CONF_USERNAME: 'john.doe',
|
||||
CONF_PASSWORD: 'secret',
|
||||
CONF_TENANT: 'eneco'
|
||||
}
|
||||
|
||||
FIXTURE_DISPLAY = {
|
||||
CONF_DISPLAY: 'display1'
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_toonapilib():
|
||||
"""Mock toonapilib."""
|
||||
with MockDependency('toonapilib') as mock_toonapilib_:
|
||||
mock_toonapilib_.Toon().display_names = [FIXTURE_DISPLAY[CONF_DISPLAY]]
|
||||
yield mock_toonapilib_
|
||||
|
||||
|
||||
async def setup_component(hass):
|
||||
"""Set up Toon component."""
|
||||
with patch('os.path.isfile', return_value=False):
|
||||
assert await async_setup_component(hass, DOMAIN, FIXTURE_APP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_abort_if_no_app_configured(hass):
|
||||
"""Test abort if no app is configured."""
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await flow.async_step_user()
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'no_app'
|
||||
|
||||
|
||||
async def test_show_authenticate_form(hass):
|
||||
"""Test that the authentication form is served."""
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'authenticate'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('side_effect,reason',
|
||||
[(InvalidConsumerKey, 'client_id'),
|
||||
(InvalidConsumerSecret, 'client_secret'),
|
||||
(AgreementsRetrievalError, 'no_agreements'),
|
||||
(Exception, 'unknown_auth_fail')])
|
||||
async def test_toon_abort(hass, mock_toonapilib, side_effect, reason):
|
||||
"""Test we abort on Toon error."""
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
mock_toonapilib.Toon.side_effect = side_effect
|
||||
|
||||
result = await flow.async_step_authenticate(user_input=FIXTURE_CREDENTIALS)
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == reason
|
||||
|
||||
|
||||
async def test_invalid_credentials(hass, mock_toonapilib):
|
||||
"""Test we show authentication form on Toon auth error."""
|
||||
mock_toonapilib.Toon.side_effect = InvalidCredentials
|
||||
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await flow.async_step_user(user_input=FIXTURE_CREDENTIALS)
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'authenticate'
|
||||
assert result['errors'] == {'base': 'credentials'}
|
||||
|
||||
|
||||
async def test_full_flow_implementation(hass, mock_toonapilib):
|
||||
"""Test registering an integration and finishing flow works."""
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'authenticate'
|
||||
|
||||
result = await flow.async_step_user(user_input=FIXTURE_CREDENTIALS)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'display'
|
||||
|
||||
result = await flow.async_step_display(user_input=FIXTURE_DISPLAY)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['title'] == FIXTURE_DISPLAY[CONF_DISPLAY]
|
||||
assert result['data'][CONF_USERNAME] == FIXTURE_CREDENTIALS[CONF_USERNAME]
|
||||
assert result['data'][CONF_PASSWORD] == FIXTURE_CREDENTIALS[CONF_PASSWORD]
|
||||
assert result['data'][CONF_TENANT] == FIXTURE_CREDENTIALS[CONF_TENANT]
|
||||
assert result['data'][CONF_DISPLAY] == FIXTURE_DISPLAY[CONF_DISPLAY]
|
||||
|
||||
|
||||
async def test_no_displays(hass, mock_toonapilib):
|
||||
"""Test abort when there are no displays."""
|
||||
await setup_component(hass)
|
||||
|
||||
mock_toonapilib.Toon().display_names = []
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
await flow.async_step_user(user_input=FIXTURE_CREDENTIALS)
|
||||
|
||||
result = await flow.async_step_display(user_input=None)
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'no_displays'
|
||||
|
||||
|
||||
async def test_display_already_exists(hass, mock_toonapilib):
|
||||
"""Test showing display form again if display already exists."""
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
await flow.async_step_user(user_input=FIXTURE_CREDENTIALS)
|
||||
|
||||
MockConfigEntry(domain=DOMAIN, data=FIXTURE_DISPLAY).add_to_hass(hass)
|
||||
|
||||
result = await flow.async_step_display(user_input=FIXTURE_DISPLAY)
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'display'
|
||||
assert result['errors'] == {'base': 'display_exists'}
|
||||
|
||||
|
||||
async def test_abort_last_minute_fail(hass, mock_toonapilib):
|
||||
"""Test we abort when API communication fails in the last step."""
|
||||
await setup_component(hass)
|
||||
|
||||
flow = config_flow.ToonFlowHandler()
|
||||
flow.hass = hass
|
||||
await flow.async_step_user(user_input=FIXTURE_CREDENTIALS)
|
||||
|
||||
mock_toonapilib.Toon.side_effect = Exception
|
||||
|
||||
result = await flow.async_step_display(user_input=FIXTURE_DISPLAY)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'unknown_auth_fail'
|
||||
Reference in new issue
Block a user