hass-core/homeassistant/components/snapcast/config_flow.py
luar123 f0710bae06
Add config-flow to Snapcast (#80288)
* initial stab at snapcast config flow

* fix linting errors

* Fix linter errors

* Add import flow, support unloading

* Add test for import flow

* Add dataclass and remove unique ID in config-flow

* remove translations

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Refactor config flow and terminate connection

* Rename test_config_flow.py

* Fix tests

* Minor fixes

* Make mock_create_server a fixture

* Combine tests

* Abort if entry already exists

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Move HomeAssistantSnapcast to own file. Clean-up last commit

* Split import flow from user flow. Fix tests.

* Use explicit asserts. Add default values to dataclass

* Change entry title to Snapcast

---------

Co-authored-by: Barrett Lowe <barrett.lowe@gmail.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-03-30 07:42:09 +02:00

63 lines
2 KiB
Python

"""Snapcast config flow."""
from __future__ import annotations
import logging
import socket
import snapcast.control
from snapcast.control.server import CONTROL_PORT
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.data_entry_flow import FlowResult
from .const import DEFAULT_TITLE, DOMAIN
_LOGGER = logging.getLogger(__name__)
SNAPCAST_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PORT, default=CONTROL_PORT): int,
}
)
class SnapcastConfigFlow(ConfigFlow, domain=DOMAIN):
"""Snapcast config flow."""
async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle first step."""
errors = {}
if user_input:
self._async_abort_entries_match(user_input)
host = user_input[CONF_HOST]
port = user_input[CONF_PORT]
# Attempt to create the server - make sure it's going to work
try:
client = await snapcast.control.create_server(
self.hass.loop, host, port, reconnect=False
)
except socket.gaierror:
errors["base"] = "invalid_host"
except OSError:
errors["base"] = "cannot_connect"
else:
await client.stop()
return self.async_create_entry(title=DEFAULT_TITLE, data=user_input)
return self.async_show_form(
step_id="user", data_schema=SNAPCAST_SCHEMA, errors=errors
)
async def async_step_import(self, import_config: dict[str, str]) -> FlowResult:
"""Import a config entry from configuration.yaml."""
self._async_abort_entries_match(
{
CONF_HOST: (import_config[CONF_HOST]),
CONF_PORT: (import_config[CONF_PORT]),
}
)
return self.async_create_entry(title=DEFAULT_TITLE, data=import_config)