Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config flow to frontier_silicon #64365

Merged
merged 37 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
427a711
Add config_flow to frontier_silicon
wlcrs Jun 29, 2022
1436787
Add missing translation file
wlcrs Jun 29, 2022
7fc104b
Delay unique_id validation until radio_id can be determined
wlcrs Jun 29, 2022
2c3456a
Fix tests
wlcrs Jun 29, 2022
f7bcbff
Improve tests
wlcrs Jun 29, 2022
65be2b6
Use FlowResultType
wlcrs Jun 29, 2022
1bae242
Bump afsapi to 0.2.6
wlcrs Jul 12, 2022
482f0e2
Fix requirements_test_all.txt
wlcrs Jul 26, 2022
44c1a28
Stash ssdp, reauth and unignore flows for now
wlcrs Aug 24, 2022
1fa59e2
Re-introduce SSDP flow
wlcrs Aug 25, 2022
5170de2
hassfest changes
wlcrs Aug 25, 2022
4a17ac8
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Oct 13, 2022
d4ca822
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Oct 14, 2022
e527195
Merge remote-tracking branch 'upstream/dev' into fsapi-0.2.0-update
wlcrs Oct 18, 2022
ddf694e
Address review comments
wlcrs Oct 18, 2022
f269111
Small style update
wlcrs Oct 18, 2022
577a6f3
Fix tests
wlcrs Oct 19, 2022
808217b
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Nov 6, 2022
447edf9
Update integrations.json
wlcrs Nov 6, 2022
12ef02b
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Nov 6, 2022
956d8b0
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Dec 16, 2022
62caf1e
Merge branch 'dev' into fsapi-0.2.0-update
wlcrs Mar 2, 2023
842f7e2
fix order in manifest.json
wlcrs Mar 2, 2023
1b8ef30
fix black errors
wlcrs Mar 2, 2023
eb5abb8
Apply suggestions from code review
wlcrs Mar 2, 2023
e087f21
Address review comments
wlcrs Mar 2, 2023
43c715e
fix black errors
wlcrs Mar 2, 2023
da0a7e1
Use async_setup_platform instead of async_setup
wlcrs Mar 6, 2023
e215fdd
Address review comments on tests
wlcrs Mar 6, 2023
1e69338
parameterize tests
wlcrs Mar 6, 2023
6fcb5c5
Remove discovery component changes from this PR
wlcrs Mar 6, 2023
c956220
Address review comments
wlcrs Mar 6, 2023
e174b53
Apply suggestions from code review
wlcrs Mar 6, 2023
382ed4f
Add extra asserts to tests
wlcrs Mar 6, 2023
df07d73
Restructure _async_step_device_config_if_needed
wlcrs Mar 6, 2023
06090ad
Add return statement
wlcrs Mar 6, 2023
08896fd
Update homeassistant/components/frontier_silicon/media_player.py
wlcrs Mar 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stash ssdp, reauth and unignore flows for now
  • Loading branch information
wlcrs committed Aug 24, 2022
commit 44c1a287672c6e3c6b9a754350617bbb57e04cff
99 changes: 2 additions & 97 deletions homeassistant/components/frontier_silicon/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
"""Config flow for Frontier Silicon Media Player integration."""
from __future__ import annotations

from collections.abc import Mapping
import logging
from typing import Any

from afsapi import AFSAPI, ConnectionError as FSConnectionError, InvalidPinException
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import ssdp
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
from homeassistant.data_entry_flow import FlowResult

from .const import (
CONF_PIN,
CONF_WEBFSAPI_URL,
DEFAULT_PIN,
DEFAULT_PORT,
DOMAIN,
SSDP_ATTR_SPEAKER_NAME,
SSDP_ST,
)
from .const import CONF_PIN, CONF_WEBFSAPI_URL, DEFAULT_PIN, DEFAULT_PORT, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,9 +44,6 @@ def __init__(self) -> None:
self._name: str | None = None
self._unique_id: str | None = None

# Only used in reauth flows:
self._reauth_entry: config_entries.ConfigEntry | None = None

async def async_step_import(self, import_info: dict[str, Any]) -> FlowResult:
"""Handle the import of legacy configuration.yaml entries."""

Expand Down Expand Up @@ -117,54 +104,7 @@ async def async_step_user(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Process entity discovered via SSDP."""

device_url = discovery_info.ssdp_location

speaker_name = discovery_info.ssdp_headers.get(SSDP_ATTR_SPEAKER_NAME)
self.context["title_placeholders"] = {"name": speaker_name}

try:
self._webfsapi_url = await AFSAPI.get_webfsapi_endpoint(device_url)
except FSConnectionError:
return self.async_abort(reason="cannot_connect")
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
return self.async_abort(reason="unknown")

# For manually added devices the unique_id is the radio_id,
# for devices discovered through SSDP it is the UDN
self._unique_id = discovery_info.ssdp_udn
await self.async_set_unique_id(self._unique_id)
self._abort_if_unique_id_configured(
updates={CONF_WEBFSAPI_URL: self._webfsapi_url}, reload_on_update=True
)

return await self._async_step_device_config_if_needed(show_confirm=True)

async def async_step_unignore(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Rediscover previously ignored devices by their unique_id."""
if not user_input or "unique_id" not in user_input:
return self.async_abort(reason="unknown")

udn = user_input.get("unique_id")
assert udn

# Find a discovery matching the unignored unique_id for a Frontier Silicon device
discovery = await ssdp.async_get_discovery_info_by_udn_st(
self.hass, udn, SSDP_ST
)
if not discovery:
return self.async_abort(reason="discovery_error")

return await self.async_step_ssdp(discovery_info=discovery)

async def _async_step_device_config_if_needed(
self, show_confirm=False
) -> FlowResult:
async def _async_step_device_config_if_needed(self) -> FlowResult:
"""Most users will not have changed the default PIN on their radio.

We try to use this default PIN, and only if this fails ask for it via `async_step_device_config`
Expand All @@ -186,26 +126,12 @@ async def _async_step_device_config_if_needed(
await self.async_set_unique_id(self._unique_id)
self._abort_if_unique_id_configured()

if show_confirm:
return await self.async_step_confirm()

return await self._create_entry()
except InvalidPinException:
pass # Ask for a PIN

return await self.async_step_device_config()

async def async_step_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Allow the user to confirm adding the device. Used when the default PIN could successfully be used."""

if user_input is not None:
return await self._create_entry()

self._set_confirm_only()
return self.async_show_form(step_id="confirm")

async def async_step_device_config(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
Expand Down Expand Up @@ -258,25 +184,4 @@ async def _create_entry(self, pin: str | None = None) -> FlowResult:

data = {CONF_WEBFSAPI_URL: self._webfsapi_url, CONF_PIN: pin or DEFAULT_PIN}

if self._reauth_entry:
self.hass.config_entries.async_update_entry(self._reauth_entry, data=data)
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")

return self.async_create_entry(title=self._name, data=data)

async def async_step_reauth(self, config: Mapping[str, Any]) -> FlowResult:
"""Perform reauth upon an API authentication error."""
assert config is not None

self._webfsapi_url = config.get(CONF_WEBFSAPI_URL)
assert self._webfsapi_url

self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)

# Set unique_id to prevent check for duplicate entity
assert self._reauth_entry
self._unique_id = self._reauth_entry.unique_id
return await self.async_step_device_config()
Loading