Add Flick Electric NZ integration (#30696)

* Add integration for Flick Electric NZ

* Start adding Config Flow and external API

* Second Wave of Config Flow and API implementation

* Fix test (errors is None instead of blank array?)

* Don't update sensor if price is still valid

* Add input validation

* Fix linting for DOMAIN

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* Remove platform schema (config is by entries only)

* Don't catch AbortFlow exception

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* Update test

* Re-arrange try-catch in config flow

* Fix linting in sensor.py

* Staticly define list of components

* Fix test exceptions

* Fix _validate_input not being awaited

* Fix tests

* Fix pylint logger

* Rename test and remove print debug

* Add test for duplicate entry

* Don't format string in log function

* Add tests __init__ file

* Remove duplicate result assignment

* Add test for generic exception handling

* Move translations folder

* Simplify testing

* Fix strings/translation

* Move to "flick_electric" as domain

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Brynley McDonaldandMartin Hjelmare authored and GitHub committed 2020-05-09 22:13:06 -04:00
1 parent 0cf1ca7736
commit e2b622fb78
14 files changed
+464

No files matched your search

@@ -0,0 +1 @@
"""Tests for the Flick Electric integration."""
@@ -0,0 +1,104 @@
"""Test the Flick Electric config flow."""
import asyncio
from asynctest import patch
from pyflick.authentication import AuthException
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.flick_electric.const import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from tests.common import MockConfigEntry
CONF = {CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"}
async def _flow_submit(hass):
return await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=CONF,
)
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
with patch(
"homeassistant.components.flick_electric.config_flow.SimpleFlickAuth.async_get_access_token",
return_value="123456789abcdef",
), patch(
"homeassistant.components.flick_electric.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.flick_electric.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], CONF,
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "Flick Electric: test-username"
assert result2["data"] == CONF
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_duplicate_login(hass):
"""Test uniqueness of username."""
entry = MockConfigEntry(
domain=DOMAIN,
data=CONF,
title="Flick Electric: test-username",
unique_id="flick_electric_test-username",
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.flick_electric.config_flow.SimpleFlickAuth.async_get_access_token",
return_value="123456789abcdef",
):
result = await _flow_submit(hass)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_form_invalid_auth(hass):
"""Test we handle invalid auth."""
with patch(
"homeassistant.components.flick_electric.config_flow.SimpleFlickAuth.async_get_access_token",
side_effect=AuthException,
):
result = await _flow_submit(hass)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
with patch(
"homeassistant.components.flick_electric.config_flow.SimpleFlickAuth.async_get_access_token",
side_effect=asyncio.TimeoutError,
):
result = await _flow_submit(hass)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "cannot_connect"}
async def test_form_generic_exception(hass):
"""Test we handle cannot connect error."""
with patch(
"homeassistant.components.flick_electric.config_flow.SimpleFlickAuth.async_get_access_token",
side_effect=Exception,
):
result = await _flow_submit(hass)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "unknown"}