hass-core/homeassistant/components/snapcast/__init__.py
luar123 f0710bae06
Add config-flow to Snapcast (#80288)
* initial stab at snapcast config flow

* fix linting errors

* Fix linter errors

* Add import flow, support unloading

* Add test for import flow

* Add dataclass and remove unique ID in config-flow

* remove translations

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Refactor config flow and terminate connection

* Rename test_config_flow.py

* Fix tests

* Minor fixes

* Make mock_create_server a fixture

* Combine tests

* Abort if entry already exists

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Move HomeAssistantSnapcast to own file. Clean-up last commit

* Split import flow from user flow. Fix tests.

* Use explicit asserts. Add default values to dataclass

* Change entry title to Snapcast

---------

Co-authored-by: Barrett Lowe <barrett.lowe@gmail.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-03-30 07:42:09 +02:00

41 lines
1.3 KiB
Python

"""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
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