Add integration scaffolding script (#26777)

* Add integration scaffolding script

* Make easier to develop

* Update py.test -> pytest
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-09-20 17:02:18 -07:00
1 parent 5cf9ba51df
commit 8502f7c7d4
15 files changed
+425

No files matched your search

@@ -0,0 +1,19 @@
"""The NEW_NAME integration."""
from .const import DOMAIN
async def async_setup(hass, config):
"""Set up the NEW_NAME integration."""
hass.data[DOMAIN] = config.get(DOMAIN, {})
return True
async def async_setup_entry(hass, entry):
"""Set up a config entry for NEW_NAME."""
# TODO forward the entry for each platform that you want to set up.
# hass.async_create_task(
# hass.config_entries.async_forward_entry_setup(entry, "media_player")
# )
return True
@@ -0,0 +1,57 @@
"""Config flow for NEW_NAME integration."""
import logging
import voluptuous as vol
from homeassistant import core, config_entries
from .const import DOMAIN # pylint:disable=unused-import
from .error import CannotConnect, InvalidAuth
_LOGGER = logging.getLogger(__name__)
# TODO adjust the data schema to the data that you need
DATA_SCHEMA = vol.Schema({"host": str, "username": str, "password": str})
async def validate_input(hass: core.HomeAssistant, data):
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
# TODO validate the data can be used to set up a connection.
# If you cannot connect:
# throw CannotConnect
# If the authentication is wrong:
# InvalidAuth
# Return some info we want to store in the config entry.
return {"title": "Name of the device"}
class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for NEW_NAME."""
VERSION = 1
# TODO pick one of the available connection classes
CONNECTION_CLASS = config_entries.CONN_CLASS_UNKNOWN
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is not None:
try:
info = await validate_input(self.hass, user_input)
return self.async_create_entry(title=info["title"], data=user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)
@@ -0,0 +1,3 @@
"""Constants for the NEW_NAME integration."""
DOMAIN = "NEW_DOMAIN"
@@ -0,0 +1,10 @@
"""Errors for the NEW_NAME integration."""
from homeassistant.exceptions import HomeAssistantError
class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""
class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""
@@ -0,0 +1,11 @@
{
"domain": "NEW_DOMAIN",
"name": "NEW_NAME",
"config_flow": true,
"documentation": "https://www.home-assistant.io/components/NEW_DOMAIN",
"requirements": ["MANIFEST_NEW_REQUIREMENT"],
"ssdp": {},
"homekit": {},
"dependencies": [],
"codeowners": ["NEW_CODEOWNER"]
}
@@ -0,0 +1,21 @@
{
"config": {
"title": "NEW_NAME",
"step": {
"user": {
"title": "Connect to the device",
"data": {
"host": "Host"
}
}
},
"error": {
"cannot_connect": "Failed to connect, please try again",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
},
"abort": {
"already_configured": "Device is already configured"
}
}
}
@@ -0,0 +1 @@
"""Tests for the NEW_NAME integration."""
@@ -0,0 +1,93 @@
"""Test the NEW_NAME config flow."""
from unittest.mock import patch
from homeassistant import config_entries, setup
from homeassistant.components.NEW_DOMAIN.const import DOMAIN
from homeassistant.components.NEW_DOMAIN.error import CannotConnect, InvalidAuth
from tests.common import mock_coro
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.NEW_DOMAIN.config_flow.validate_input",
return_value=mock_coro({"title": "Test Title"}),
), patch(
"homeassistant.components.NEW_DOMAIN.async_setup", return_value=mock_coro(True)
) as mock_setup, patch(
"homeassistant.components.NEW_DOMAIN.async_setup_entry",
return_value=mock_coro(True),
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "Test Title"
assert result2["data"] == {
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
}
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_invalid_auth(hass):
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
side_effect=InvalidAuth,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
side_effect=CannotConnect,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}