-
-
Notifications
You must be signed in to change notification settings - Fork 38k
Add config-flow to Snapcast #80288
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
Merged
Merged
Add config-flow to Snapcast #80288
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1f4ec26
initial stab at snapcast config flow
BarrettLowe 878c4ea
fix linting errors
BarrettLowe 0046e98
Merge remote-tracking branch 'BarrettLowe/snapcast_config_flow' into …
luar123 7140820
Fix linter errors
luar123 45610fa
Add import flow, support unloading
luar123 9d6e529
Add test for import flow
luar123 8d50595
Add dataclass and remove unique ID in config-flow
luar123 84f493c
Merge remote-tracking branch 'upstream/dev' into snapcast_configflow
luar123 4b52865
remove translations
luar123 411fa78
Merge remote-tracking branch 'upstream/dev' into snapcast_configflow
luar123 b9dbc5b
Apply suggestions from code review
luar123 b9a57d2
Refactor config flow and terminate connection
luar123 7c97460
Rename test_config_flow.py
luar123 ec67248
Fix tests
luar123 50580e9
Minor fixes
luar123 d520968
Make mock_create_server a fixture
luar123 5ac9af4
Combine tests
luar123 dec6cda
Merge remote-tracking branch 'upstream/dev' into snapcast_configflow
luar123 cb62c3f
Abort if entry already exists
luar123 69a902f
Apply suggestions from code review
luar123 2d3ad78
Move HomeAssistantSnapcast to own file. Clean-up last commit
luar123 7df4d7a
Split import flow from user flow. Fix tests.
luar123 a1ec736
Use explicit asserts. Add default values to dataclass
luar123 8304c14
Change entry title to Snapcast
luar123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 +1,41 @@ | ||
| """The snapcast component.""" | ||
| """Snapcast Integration.""" | ||
| import logging | ||
|
|
||
| import snapcast.control | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_HOST, CONF_PORT | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ConfigEntryNotReady | ||
|
|
||
| from .const import DOMAIN, PLATFORMS | ||
| from .server import HomeAssistantSnapcast | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Set up Snapcast from a config entry.""" | ||
| host = entry.data[CONF_HOST] | ||
| port = entry.data[CONF_PORT] | ||
| try: | ||
| server = await snapcast.control.create_server( | ||
| hass.loop, host, port, reconnect=True | ||
| ) | ||
| except OSError as ex: | ||
| raise ConfigEntryNotReady( | ||
| f"Could not connect to Snapcast server at {host}:{port}" | ||
| ) from ex | ||
|
|
||
|
luar123 marked this conversation as resolved.
|
||
| hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantSnapcast(server) | ||
|
|
||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Unload a config entry.""" | ||
| if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
| hass.data[DOMAIN].pop(entry.entry_id) | ||
| return unload_ok | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """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) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Snapcast Integration.""" | ||
| from dataclasses import dataclass, field | ||
|
|
||
| from snapcast.control import Snapserver | ||
|
|
||
| from homeassistant.components.media_player import MediaPlayerEntity | ||
|
|
||
|
|
||
| @dataclass | ||
| class HomeAssistantSnapcast: | ||
| """Snapcast data stored in the Home Assistant data object.""" | ||
|
|
||
| server: Snapserver | ||
| clients: list[MediaPlayerEntity] = field(default_factory=list) | ||
| groups: list[MediaPlayerEntity] = field(default_factory=list) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "config": { | ||
| "step": { | ||
| "user": { | ||
| "description": "Please enter your server connection details", | ||
| "data": { | ||
| "host": "[%key:common::config_flow::data::host%]", | ||
| "port": "[%key:common::config_flow::data::port%]" | ||
| }, | ||
| "title": "Connect" | ||
| } | ||
| }, | ||
| "abort": { | ||
| "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", | ||
| "invalid_host": "[%key:common::config_flow::error::invalid_host%]" | ||
| } | ||
| }, | ||
| "issues": { | ||
| "deprecated_yaml": { | ||
| "title": "The Snapcast YAML configuration is being removed", | ||
| "description": "Configuring Snapcast using YAML is being removed.\n\nYour existing YAML configuration has been imported into the UI automatically.\n\nRemove the Snapcast YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue." | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -397,6 +397,7 @@ | |
| "smarttub", | ||
| "smhi", | ||
| "sms", | ||
| "snapcast", | ||
| "snooz", | ||
| "solaredge", | ||
| "solarlog", | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Snapcast integration.""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.