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

Merged config_flow HA changes #15

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 18 additions & 14 deletions custom_components/monoprice_custom/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow for Monoprice 6-Zone Amplifier integration."""

from __future__ import annotations

import logging
Expand All @@ -7,8 +8,10 @@
from serial import SerialException
import voluptuous as vol

from homeassistant import config_entries, core, exceptions
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_PORT
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError

from .const import (
CONF_SOURCE_1,
Expand All @@ -33,9 +36,11 @@
]

OPTIONS_FOR_DATA = {vol.Optional(source): str for source in SOURCES}

DATA_SCHEMA = vol.Schema({vol.Required(CONF_PORT): str, **OPTIONS_FOR_DATA})

@core.callback

@callback
def _sources_from_config(data):
sources_config = {
str(idx + 1): data.get(source) for idx, source in enumerate(SOURCES)
Expand All @@ -48,25 +53,24 @@ def _sources_from_config(data):
}


async def validate_input(hass: core.HomeAssistant, data):
async def validate_input(hass: HomeAssistant, data):
"""Validate the user input allows us to connect.

Data has the keys from DATA_SCHEMA with values provided by the user.
"""
try:
await hass.async_add_executor_job(get_monoprice, data[CONF_PORT])
except SerialException as err:
_LOGGER.error("Error connecting to Monoprice controller %s", data[CONF_PORT])
_LOGGER.error("Error connecting to Monoprice controller")
raise CannotConnect from err

sources = _sources_from_config(data)


# Return info that you want to store in the config entry.
return {CONF_PORT: data[CONF_PORT], CONF_SOURCES: sources}


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class MonoPriceConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Monoprice 6-Zone Amplifier."""

VERSION = 1
Expand All @@ -90,15 +94,15 @@ async def async_step_user(self, user_input=None):
)

@staticmethod
@core.callback
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
config_entry: ConfigEntry,
) -> MonopriceOptionsFlowHandler:
"""Define the config flow to handle options."""
return MonopriceOptionsFlowHandler(config_entry)


@core.callback
@callback
def _key_for_source(index, source, previous_sources):
if str(index) in previous_sources:
key = vol.Optional(
Expand All @@ -110,14 +114,14 @@ def _key_for_source(index, source, previous_sources):
return key


class MonopriceOptionsFlowHandler(config_entries.OptionsFlow):
class MonopriceOptionsFlowHandler(OptionsFlow):
"""Handle a Monoprice options flow."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize."""
self.config_entry = config_entry

@core.callback
@callback
def _previous_sources(self):
if CONF_SOURCES in self.config_entry.options:
previous = self.config_entry.options[CONF_SOURCES]
Expand Down Expand Up @@ -146,5 +150,5 @@ async def async_step_init(self, user_input=None):
)


class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""
class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""
1 change: 0 additions & 1 deletion custom_components/monoprice_custom/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import voluptuous as vol

from .const import (
CONF_NAME,
CONF_SOURCES,
DOMAIN,
FIRST_RUN,
Expand Down