forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arcam config flow (home-assistant#34384)
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
- Loading branch information
1 parent
524b48b
commit 31973de
Showing
14 changed files
with
378 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,102 @@ | ||
"""Config flow to configure the Arcam FMJ component.""" | ||
from operator import itemgetter | ||
import logging | ||
from urllib.parse import urlparse | ||
|
||
from arcam.fmj.client import Client, ConnectionFailed | ||
from arcam.fmj.utils import get_uniqueid_from_host, get_uniqueid_from_udn | ||
import voluptuous as vol | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.components.ssdp import ATTR_SSDP_LOCATION, ATTR_UPNP_UDN | ||
from homeassistant.const import CONF_HOST, CONF_PORT | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
||
from .const import DEFAULT_NAME, DEFAULT_PORT, DOMAIN, DOMAIN_DATA_ENTRIES | ||
|
||
from .const import DOMAIN | ||
_LOGGER = logging.getLogger(__name__) | ||
|
||
_GETKEY = itemgetter(CONF_HOST, CONF_PORT) | ||
|
||
def get_entry_client(hass, entry): | ||
"""Retrieve client associated with a config entry.""" | ||
return hass.data[DOMAIN_DATA_ENTRIES][entry.entry_id] | ||
|
||
|
||
@config_entries.HANDLERS.register(DOMAIN) | ||
class ArcamFmjFlowHandler(config_entries.ConfigFlow): | ||
"""Handle a SimpliSafe config flow.""" | ||
"""Handle config flow.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL | ||
|
||
async def async_step_import(self, import_config): | ||
"""Import a config entry from configuration.yaml.""" | ||
entries = self.hass.config_entries.async_entries(DOMAIN) | ||
import_key = _GETKEY(import_config) | ||
for entry in entries: | ||
if _GETKEY(entry.data) == import_key: | ||
return self.async_abort(reason="already_setup") | ||
async def _async_set_unique_id_and_update(self, host, port, uuid): | ||
await self.async_set_unique_id(uuid) | ||
self._abort_if_unique_id_configured({CONF_HOST: host, CONF_PORT: port}) | ||
|
||
async def _async_check_and_create(self, host, port): | ||
client = Client(host, port) | ||
try: | ||
await client.start() | ||
except ConnectionFailed: | ||
return self.async_abort(reason="unable_to_connect") | ||
finally: | ||
await client.stop() | ||
|
||
return self.async_create_entry( | ||
title=f"{DEFAULT_NAME} ({host})", data={CONF_HOST: host, CONF_PORT: port}, | ||
) | ||
|
||
async def async_step_user(self, user_info=None): | ||
"""Handle a discovered device.""" | ||
errors = {} | ||
|
||
if user_info is not None: | ||
uuid = await get_uniqueid_from_host( | ||
async_get_clientsession(self.hass), user_info[CONF_HOST] | ||
) | ||
if uuid: | ||
await self._async_set_unique_id_and_update( | ||
user_info[CONF_HOST], user_info[CONF_PORT], uuid | ||
) | ||
|
||
return await self._async_check_and_create( | ||
user_info[CONF_HOST], user_info[CONF_PORT] | ||
) | ||
|
||
fields = { | ||
vol.Required(CONF_HOST): str, | ||
vol.Required(CONF_PORT, default=DEFAULT_PORT): int, | ||
} | ||
|
||
return self.async_show_form( | ||
step_id="user", data_schema=vol.Schema(fields), errors=errors | ||
) | ||
|
||
async def async_step_confirm(self, user_input=None): | ||
"""Handle user-confirmation of discovered node.""" | ||
context = self.context # pylint: disable=no-member | ||
placeholders = { | ||
"host": context[CONF_HOST], | ||
} | ||
context["title_placeholders"] = placeholders | ||
|
||
if user_input is not None: | ||
return await self._async_check_and_create( | ||
context[CONF_HOST], context[CONF_PORT] | ||
) | ||
|
||
return self.async_show_form( | ||
step_id="confirm", description_placeholders=placeholders | ||
) | ||
|
||
async def async_step_ssdp(self, discovery_info): | ||
"""Handle a discovered device.""" | ||
host = urlparse(discovery_info[ATTR_SSDP_LOCATION]).hostname | ||
port = DEFAULT_PORT | ||
uuid = get_uniqueid_from_udn(discovery_info[ATTR_UPNP_UDN]) | ||
|
||
await self._async_set_unique_id_and_update(host, port, uuid) | ||
|
||
return self.async_create_entry(title="Arcam FMJ", data=import_config) | ||
context = self.context # pylint: disable=no-member | ||
context[CONF_HOST] = host | ||
context[CONF_PORT] = DEFAULT_PORT | ||
return await self.async_step_confirm() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
{ | ||
"domain": "arcam_fmj", | ||
"name": "Arcam FMJ Receivers", | ||
"config_flow": false, | ||
"config_flow": true, | ||
"documentation": "https://www.home-assistant.io/integrations/arcam_fmj", | ||
"requirements": ["arcam-fmj==0.4.6"], | ||
"requirements": ["arcam-fmj==0.5.1"], | ||
"ssdp": [ | ||
{ | ||
"deviceType": "urn:schemas-upnp-org:device:MediaRenderer:1", | ||
"manufacturer": "ARCAM" | ||
} | ||
], | ||
"codeowners": ["@elupus"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.