2019-03-29 23:10:00 -05:00
|
|
|
"""Config flow to configure Heos."""
|
2019-12-19 19:28:03 +02:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2019-08-25 13:57:43 -05:00
|
|
|
from pyheos import Heos, HeosError
|
2019-03-30 08:52:17 -05:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-29 23:10:00 -05:00
|
|
|
from homeassistant import config_entries
|
2019-12-19 19:28:03 +02:00
|
|
|
from homeassistant.components import ssdp
|
|
|
|
from homeassistant.const import CONF_HOST
|
2019-03-29 23:10:00 -05:00
|
|
|
|
2019-04-13 16:44:45 -05:00
|
|
|
from .const import DATA_DISCOVERED_HOSTS, DOMAIN
|
2019-03-29 23:10:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
def format_title(host: str) -> str:
|
|
|
|
"""Format the title for config entries."""
|
2019-09-03 17:27:14 +02:00
|
|
|
return f"Controller ({host})"
|
2019-03-29 23:10:00 -05:00
|
|
|
|
|
|
|
|
2021-04-30 23:28:25 +02:00
|
|
|
class HeosFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
2019-03-29 23:10:00 -05:00
|
|
|
"""Define a flow for HEOS."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2019-06-12 16:08:08 -07:00
|
|
|
async def async_step_ssdp(self, discovery_info):
|
2019-04-02 15:22:49 -05:00
|
|
|
"""Handle a discovered Heos device."""
|
2019-04-13 16:44:45 -05:00
|
|
|
# Store discovered host
|
2019-12-19 19:28:03 +02:00
|
|
|
hostname = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]).hostname
|
2020-02-28 12:39:29 +01:00
|
|
|
friendly_name = f"{discovery_info[ssdp.ATTR_UPNP_FRIENDLY_NAME]} ({hostname})"
|
2019-04-13 16:44:45 -05:00
|
|
|
self.hass.data.setdefault(DATA_DISCOVERED_HOSTS, {})
|
2019-12-19 19:28:03 +02:00
|
|
|
self.hass.data[DATA_DISCOVERED_HOSTS][friendly_name] = hostname
|
2019-04-13 16:44:45 -05:00
|
|
|
# Abort if other flows in progress or an entry already exists
|
|
|
|
if self._async_in_progress() or self._async_current_entries():
|
2020-10-05 19:22:02 +02:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2020-05-26 10:51:50 -05:00
|
|
|
await self.async_set_unique_id(DOMAIN)
|
2019-04-13 16:44:45 -05:00
|
|
|
# Show selection form
|
2019-07-31 12:25:30 -07:00
|
|
|
return self.async_show_form(step_id="user")
|
2019-04-02 15:22:49 -05:00
|
|
|
|
2019-03-29 23:10:00 -05:00
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Occurs when an entry is setup through config."""
|
|
|
|
host = user_input[CONF_HOST]
|
2020-05-26 10:51:50 -05:00
|
|
|
# raise_on_progress is False here in case ssdp discovers
|
|
|
|
# heos first which would block the import
|
|
|
|
await self.async_set_unique_id(DOMAIN, raise_on_progress=False)
|
2019-07-31 12:25:30 -07:00
|
|
|
return self.async_create_entry(title=format_title(host), data={CONF_HOST: host})
|
2019-03-30 08:52:17 -05:00
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Obtain host and validate connection."""
|
2019-04-13 16:44:45 -05:00
|
|
|
self.hass.data.setdefault(DATA_DISCOVERED_HOSTS, {})
|
2019-04-02 15:22:49 -05:00
|
|
|
# Only a single entry is needed for all devices
|
2019-04-13 16:44:45 -05:00
|
|
|
if self._async_current_entries():
|
2020-10-05 19:22:02 +02:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2019-03-30 08:52:17 -05:00
|
|
|
# Try connecting to host if provided
|
|
|
|
errors = {}
|
|
|
|
host = None
|
|
|
|
if user_input is not None:
|
|
|
|
host = user_input[CONF_HOST]
|
2019-04-13 16:44:45 -05:00
|
|
|
# Map host from friendly name if in discovered hosts
|
|
|
|
host = self.hass.data[DATA_DISCOVERED_HOSTS].get(host, host)
|
2019-03-30 08:52:17 -05:00
|
|
|
heos = Heos(host)
|
|
|
|
try:
|
|
|
|
await heos.connect()
|
2019-04-13 16:44:45 -05:00
|
|
|
self.hass.data.pop(DATA_DISCOVERED_HOSTS)
|
|
|
|
return await self.async_step_import({CONF_HOST: host})
|
2019-08-25 13:57:43 -05:00
|
|
|
except HeosError:
|
2020-10-05 19:22:02 +02:00
|
|
|
errors[CONF_HOST] = "cannot_connect"
|
2019-03-30 08:52:17 -05:00
|
|
|
finally:
|
|
|
|
await heos.disconnect()
|
|
|
|
|
|
|
|
# Return form
|
2019-07-31 12:25:30 -07:00
|
|
|
host_type = (
|
|
|
|
str
|
|
|
|
if not self.hass.data[DATA_DISCOVERED_HOSTS]
|
2019-04-13 16:44:45 -05:00
|
|
|
else vol.In(list(self.hass.data[DATA_DISCOVERED_HOSTS]))
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-03-30 08:52:17 -05:00
|
|
|
return self.async_show_form(
|
2019-07-31 12:25:30 -07:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST, default=host): host_type}),
|
|
|
|
errors=errors,
|
|
|
|
)
|