2019-03-30 04:10:00 +00:00
|
|
|
"""Config flow to configure Heos."""
|
2019-03-30 13:52:17 +00:00
|
|
|
import asyncio
|
|
|
|
|
2019-05-26 11:47:11 +00:00
|
|
|
from pyheos import Heos
|
2019-03-30 13:52:17 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-30 04:10:00 +00:00
|
|
|
from homeassistant import config_entries
|
2019-04-13 21:44:45 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2019-03-30 04:10:00 +00:00
|
|
|
|
2019-04-13 21:44:45 +00:00
|
|
|
from .const import DATA_DISCOVERED_HOSTS, DOMAIN
|
2019-03-30 04:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_title(host: str) -> str:
|
|
|
|
"""Format the title for config entries."""
|
|
|
|
return "Controller ({})".format(host)
|
|
|
|
|
|
|
|
|
|
|
|
@config_entries.HANDLERS.register(DOMAIN)
|
|
|
|
class HeosFlowHandler(config_entries.ConfigFlow):
|
|
|
|
"""Define a flow for HEOS."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
|
|
|
|
2019-06-12 23:08:08 +00:00
|
|
|
async def async_step_ssdp(self, discovery_info):
|
2019-04-02 20:22:49 +00:00
|
|
|
"""Handle a discovered Heos device."""
|
2019-04-13 21:44:45 +00:00
|
|
|
# Store discovered host
|
|
|
|
friendly_name = "{} ({})".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
discovery_info[CONF_NAME], discovery_info[CONF_HOST]
|
|
|
|
)
|
2019-04-13 21:44:45 +00:00
|
|
|
self.hass.data.setdefault(DATA_DISCOVERED_HOSTS, {})
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.data[DATA_DISCOVERED_HOSTS][friendly_name] = discovery_info[CONF_HOST]
|
2019-04-13 21:44:45 +00:00
|
|
|
# Abort if other flows in progress or an entry already exists
|
|
|
|
if self._async_in_progress() or self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="already_setup")
|
2019-04-13 21:44:45 +00:00
|
|
|
# Show selection form
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="user")
|
2019-04-02 20:22:49 +00:00
|
|
|
|
2019-03-30 04:10:00 +00:00
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Occurs when an entry is setup through config."""
|
|
|
|
host = user_input[CONF_HOST]
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=format_title(host), data={CONF_HOST: host})
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Obtain host and validate connection."""
|
2019-04-13 21:44:45 +00:00
|
|
|
self.hass.data.setdefault(DATA_DISCOVERED_HOSTS, {})
|
2019-04-02 20:22:49 +00:00
|
|
|
# Only a single entry is needed for all devices
|
2019-04-13 21:44:45 +00:00
|
|
|
if self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="already_setup")
|
2019-03-30 13:52:17 +00:00
|
|
|
# Try connecting to host if provided
|
|
|
|
errors = {}
|
|
|
|
host = None
|
|
|
|
if user_input is not None:
|
|
|
|
host = user_input[CONF_HOST]
|
2019-04-13 21:44:45 +00:00
|
|
|
# Map host from friendly name if in discovered hosts
|
|
|
|
host = self.hass.data[DATA_DISCOVERED_HOSTS].get(host, host)
|
2019-03-30 13:52:17 +00:00
|
|
|
heos = Heos(host)
|
|
|
|
try:
|
|
|
|
await heos.connect()
|
2019-04-13 21:44:45 +00:00
|
|
|
self.hass.data.pop(DATA_DISCOVERED_HOSTS)
|
|
|
|
return await self.async_step_import({CONF_HOST: host})
|
2019-03-30 13:52:17 +00:00
|
|
|
except (asyncio.TimeoutError, ConnectionError):
|
2019-07-31 19:25:30 +00:00
|
|
|
errors[CONF_HOST] = "connection_failure"
|
2019-03-30 13:52:17 +00:00
|
|
|
finally:
|
|
|
|
await heos.disconnect()
|
|
|
|
|
|
|
|
# Return form
|
2019-07-31 19:25:30 +00:00
|
|
|
host_type = (
|
|
|
|
str
|
|
|
|
if not self.hass.data[DATA_DISCOVERED_HOSTS]
|
2019-04-13 21:44:45 +00:00
|
|
|
else vol.In(list(self.hass.data[DATA_DISCOVERED_HOSTS]))
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-30 13:52:17 +00:00
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST, default=host): host_type}),
|
|
|
|
errors=errors,
|
|
|
|
)
|