Add Hue manual bridge config flow + options flow (#37268)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
1 parent
cf3f755edc
commit
235298a1b2
9 files changed
+389
-117
No files matched your search
@@ -31,26 +31,25 @@ BRIDGE_CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
# Validate as IP address and then convert back to a string.
|
||||
vol.Required(CONF_HOST): vol.All(ipaddress.ip_address, cv.string),
|
||||
vol.Optional(
|
||||
CONF_ALLOW_UNREACHABLE, default=DEFAULT_ALLOW_UNREACHABLE
|
||||
): cv.boolean,
|
||||
vol.Optional(
|
||||
CONF_ALLOW_HUE_GROUPS, default=DEFAULT_ALLOW_HUE_GROUPS
|
||||
): cv.boolean,
|
||||
vol.Optional(CONF_ALLOW_UNREACHABLE): cv.boolean,
|
||||
vol.Optional(CONF_ALLOW_HUE_GROUPS): cv.boolean,
|
||||
vol.Optional("filename"): str,
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_BRIDGES): vol.All(
|
||||
cv.ensure_list, [BRIDGE_CONFIG_SCHEMA],
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
vol.All(
|
||||
cv.deprecated(DOMAIN, invalidation_version="0.115.0"),
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_BRIDGES): vol.All(
|
||||
cv.ensure_list, [BRIDGE_CONFIG_SCHEMA],
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
),
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
@@ -64,7 +63,7 @@ async def async_setup(hass, config):
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DATA_CONFIGS] = {}
|
||||
|
||||
# User has configured bridges
|
||||
# User has not configured bridges
|
||||
if CONF_BRIDGES not in conf:
|
||||
return True
|
||||
|
||||
@@ -105,16 +104,55 @@ async def async_setup_entry(
|
||||
host = entry.data["host"]
|
||||
config = hass.data[DATA_CONFIGS].get(host)
|
||||
|
||||
if config is None:
|
||||
allow_unreachable = entry.data.get(
|
||||
CONF_ALLOW_UNREACHABLE, DEFAULT_ALLOW_UNREACHABLE
|
||||
)
|
||||
allow_groups = entry.data.get(CONF_ALLOW_HUE_GROUPS, DEFAULT_ALLOW_HUE_GROUPS)
|
||||
else:
|
||||
allow_unreachable = config[CONF_ALLOW_UNREACHABLE]
|
||||
allow_groups = config[CONF_ALLOW_HUE_GROUPS]
|
||||
# Migrate allow_unreachable from config entry data to config entry options
|
||||
if (
|
||||
CONF_ALLOW_UNREACHABLE not in entry.options
|
||||
and CONF_ALLOW_UNREACHABLE in entry.data
|
||||
and entry.data[CONF_ALLOW_UNREACHABLE] != DEFAULT_ALLOW_UNREACHABLE
|
||||
):
|
||||
options = {
|
||||
**entry.options,
|
||||
CONF_ALLOW_UNREACHABLE: entry.data[CONF_ALLOW_UNREACHABLE],
|
||||
}
|
||||
data = entry.data.copy()
|
||||
data.pop(CONF_ALLOW_UNREACHABLE)
|
||||
hass.config_entries.async_update_entry(entry, data=data, options=options)
|
||||
|
||||
bridge = HueBridge(hass, entry, allow_unreachable, allow_groups)
|
||||
# Migrate allow_hue_groups from config entry data to config entry options
|
||||
if (
|
||||
CONF_ALLOW_HUE_GROUPS not in entry.options
|
||||
and CONF_ALLOW_HUE_GROUPS in entry.data
|
||||
and entry.data[CONF_ALLOW_HUE_GROUPS] != DEFAULT_ALLOW_HUE_GROUPS
|
||||
):
|
||||
options = {
|
||||
**entry.options,
|
||||
CONF_ALLOW_HUE_GROUPS: entry.data[CONF_ALLOW_HUE_GROUPS],
|
||||
}
|
||||
data = entry.data.copy()
|
||||
data.pop(CONF_ALLOW_HUE_GROUPS)
|
||||
hass.config_entries.async_update_entry(entry, data=data, options=options)
|
||||
|
||||
# Overwrite from YAML configuration
|
||||
if config is not None:
|
||||
options = {}
|
||||
if CONF_ALLOW_HUE_GROUPS in config and (
|
||||
CONF_ALLOW_HUE_GROUPS not in entry.options
|
||||
or config[CONF_ALLOW_HUE_GROUPS] != entry.options[CONF_ALLOW_HUE_GROUPS]
|
||||
):
|
||||
options[CONF_ALLOW_HUE_GROUPS] = config[CONF_ALLOW_HUE_GROUPS]
|
||||
|
||||
if CONF_ALLOW_UNREACHABLE in config and (
|
||||
CONF_ALLOW_UNREACHABLE not in entry.options
|
||||
or config[CONF_ALLOW_UNREACHABLE] != entry.options[CONF_ALLOW_UNREACHABLE]
|
||||
):
|
||||
options[CONF_ALLOW_UNREACHABLE] = config[CONF_ALLOW_UNREACHABLE]
|
||||
|
||||
if options:
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, options={**entry.options, **options},
|
||||
)
|
||||
|
||||
bridge = HueBridge(hass, entry)
|
||||
|
||||
if not await bridge.async_setup():
|
||||
return False
|
||||
|
||||
@@ -14,7 +14,14 @@ from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .const import (
|
||||
CONF_ALLOW_HUE_GROUPS,
|
||||
CONF_ALLOW_UNREACHABLE,
|
||||
DEFAULT_ALLOW_HUE_GROUPS,
|
||||
DEFAULT_ALLOW_UNREACHABLE,
|
||||
DOMAIN,
|
||||
LOGGER,
|
||||
)
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
from .helpers import create_config_flow
|
||||
from .sensor_base import SensorManager
|
||||
@@ -33,12 +40,10 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class HueBridge:
|
||||
"""Manages a single Hue bridge."""
|
||||
|
||||
def __init__(self, hass, config_entry, allow_unreachable, allow_groups):
|
||||
def __init__(self, hass, config_entry):
|
||||
"""Initialize the system."""
|
||||
self.config_entry = config_entry
|
||||
self.hass = hass
|
||||
self.allow_unreachable = allow_unreachable
|
||||
self.allow_groups = allow_groups
|
||||
self.available = True
|
||||
self.authorized = False
|
||||
self.api = None
|
||||
@@ -46,12 +51,27 @@ class HueBridge:
|
||||
# Jobs to be executed when API is reset.
|
||||
self.reset_jobs = []
|
||||
self.sensor_manager = None
|
||||
self.unsub_config_entry_listner = None
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
"""Return the host of this bridge."""
|
||||
return self.config_entry.data["host"]
|
||||
|
||||
@property
|
||||
def allow_unreachable(self):
|
||||
"""Allow unreachable light bulbs."""
|
||||
return self.config_entry.options.get(
|
||||
CONF_ALLOW_UNREACHABLE, DEFAULT_ALLOW_UNREACHABLE
|
||||
)
|
||||
|
||||
@property
|
||||
def allow_groups(self):
|
||||
"""Allow groups defined in the Hue bridge."""
|
||||
return self.config_entry.options.get(
|
||||
CONF_ALLOW_HUE_GROUPS, DEFAULT_ALLOW_HUE_GROUPS
|
||||
)
|
||||
|
||||
async def async_setup(self, tries=0):
|
||||
"""Set up a phue bridge based on host parameter."""
|
||||
host = self.host
|
||||
@@ -105,6 +125,10 @@ class HueBridge:
|
||||
3 if self.api.config.modelid == "BSB001" else 10
|
||||
)
|
||||
|
||||
self.unsub_config_entry_listner = self.config_entry.add_update_listener(
|
||||
_update_listener
|
||||
)
|
||||
|
||||
self.authorized = True
|
||||
return True
|
||||
|
||||
@@ -160,6 +184,9 @@ class HueBridge:
|
||||
while self.reset_jobs:
|
||||
self.reset_jobs.pop()()
|
||||
|
||||
if self.unsub_config_entry_listner is not None:
|
||||
self.unsub_config_entry_listner()
|
||||
|
||||
# If setup was successful, we set api variable, forwarded entry and
|
||||
# register service
|
||||
results = await asyncio.gather(
|
||||
@@ -244,8 +271,18 @@ async def authenticate_bridge(hass: core.HomeAssistant, bridge: aiohue.Bridge):
|
||||
|
||||
except (aiohue.LinkButtonNotPressed, aiohue.Unauthorized):
|
||||
raise AuthenticationRequired
|
||||
except (asyncio.TimeoutError, client_exceptions.ClientOSError):
|
||||
except (
|
||||
asyncio.TimeoutError,
|
||||
client_exceptions.ClientOSError,
|
||||
client_exceptions.ServerDisconnectedError,
|
||||
client_exceptions.ContentTypeError,
|
||||
):
|
||||
raise CannotConnect
|
||||
except aiohue.AiohueException:
|
||||
LOGGER.exception("Unknown Hue linking error occurred")
|
||||
raise AuthenticationRequired
|
||||
|
||||
|
||||
async def _update_listener(hass, entry):
|
||||
"""Handle options update."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Config flow to configure Philips Hue."""
|
||||
import asyncio
|
||||
from typing import Dict, Optional
|
||||
from typing import Any, Dict, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import aiohue
|
||||
@@ -10,12 +10,14 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant.components import ssdp
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.const import CONF_HOST, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .bridge import authenticate_bridge
|
||||
from .const import ( # pylint: disable=unused-import
|
||||
CONF_ALLOW_HUE_GROUPS,
|
||||
CONF_ALLOW_UNREACHABLE,
|
||||
DOMAIN,
|
||||
LOGGER,
|
||||
)
|
||||
@@ -23,6 +25,7 @@ from .errors import AuthenticationRequired, CannotConnect
|
||||
|
||||
HUE_MANUFACTURERURL = "http://www.philips.com"
|
||||
HUE_IGNORED_BRIDGE_NAMES = ["Home Assistant Bridge", "Espalexa"]
|
||||
HUE_MANUAL_BRIDGE_ID = "manual"
|
||||
|
||||
|
||||
class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
@@ -31,7 +34,11 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
||||
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry):
|
||||
"""Get the options flow for this handler."""
|
||||
return HueOptionsFlowHandler(config_entry)
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the Hue flow."""
|
||||
@@ -57,6 +64,10 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
"""Handle a flow start."""
|
||||
# Check if user chooses manual entry
|
||||
if user_input is not None and user_input["id"] == HUE_MANUAL_BRIDGE_ID:
|
||||
return await self.async_step_manual()
|
||||
|
||||
if (
|
||||
user_input is not None
|
||||
and self.discovered_bridges is not None
|
||||
@@ -64,9 +75,9 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
):
|
||||
self.bridge = self.discovered_bridges[user_input["id"]]
|
||||
await self.async_set_unique_id(self.bridge.id, raise_on_progress=False)
|
||||
# We pass user input to link so it will attempt to link right away
|
||||
return await self.async_step_link({})
|
||||
return await self.async_step_link()
|
||||
|
||||
# Find / discover bridges
|
||||
try:
|
||||
with async_timeout.timeout(5):
|
||||
bridges = await discover_nupnp(
|
||||
@@ -75,34 +86,50 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
except asyncio.TimeoutError:
|
||||
return self.async_abort(reason="discover_timeout")
|
||||
|
||||
if not bridges:
|
||||
return self.async_abort(reason="no_bridges")
|
||||
if bridges:
|
||||
# Find already configured hosts
|
||||
already_configured = self._async_current_ids(False)
|
||||
bridges = [
|
||||
bridge for bridge in bridges if bridge.id not in already_configured
|
||||
]
|
||||
self.discovered_bridges = {bridge.id: bridge for bridge in bridges}
|
||||
|
||||
# Find already configured hosts
|
||||
already_configured = self._async_current_ids(False)
|
||||
bridges = [bridge for bridge in bridges if bridge.id not in already_configured]
|
||||
|
||||
if not bridges:
|
||||
return self.async_abort(reason="all_configured")
|
||||
|
||||
if len(bridges) == 1:
|
||||
self.bridge = bridges[0]
|
||||
await self.async_set_unique_id(self.bridge.id, raise_on_progress=False)
|
||||
return await self.async_step_link()
|
||||
|
||||
self.discovered_bridges = {bridge.id: bridge for bridge in bridges}
|
||||
if not self.discovered_bridges:
|
||||
return await self.async_step_manual()
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required("id"): vol.In(
|
||||
{bridge.id: bridge.host for bridge in bridges}
|
||||
{
|
||||
**{bridge.id: bridge.host for bridge in bridges},
|
||||
HUE_MANUAL_BRIDGE_ID: "Manually add a Hue Bridge",
|
||||
}
|
||||
)
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
async def async_step_manual(
|
||||
self, user_input: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle manual bridge setup."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="manual",
|
||||
data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
|
||||
)
|
||||
|
||||
if any(
|
||||
user_input["host"] == entry.data["host"]
|
||||
for entry in self._async_current_entries()
|
||||
):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
self.bridge = self._async_get_bridge(user_input[CONF_HOST])
|
||||
return await self.async_step_link()
|
||||
|
||||
async def async_step_link(self, user_input=None):
|
||||
"""Attempt to link with the Hue bridge.
|
||||
|
||||
@@ -118,35 +145,30 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
try:
|
||||
await authenticate_bridge(self.hass, bridge)
|
||||
|
||||
# Can happen if we come from import.
|
||||
if self.unique_id is None:
|
||||
await self.async_set_unique_id(
|
||||
normalize_bridge_id(bridge.id), raise_on_progress=False
|
||||
)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=bridge.config.name,
|
||||
data={
|
||||
"host": bridge.host,
|
||||
"username": bridge.username,
|
||||
CONF_ALLOW_HUE_GROUPS: False,
|
||||
},
|
||||
)
|
||||
except AuthenticationRequired:
|
||||
errors["base"] = "register_failed"
|
||||
|
||||
except CannotConnect:
|
||||
LOGGER.error("Error connecting to the Hue bridge at %s", bridge.host)
|
||||
errors["base"] = "linking"
|
||||
|
||||
return self.async_abort(reason="cannot_connect")
|
||||
except Exception: # pylint: disable=broad-except
|
||||
LOGGER.exception(
|
||||
"Unknown error connecting with Hue bridge at %s", bridge.host
|
||||
)
|
||||
errors["base"] = "linking"
|
||||
|
||||
return self.async_show_form(step_id="link", errors=errors)
|
||||
if errors:
|
||||
return self.async_show_form(step_id="link", errors=errors)
|
||||
|
||||
# Can happen if we come from import or manual entry
|
||||
if self.unique_id is None:
|
||||
await self.async_set_unique_id(
|
||||
normalize_bridge_id(bridge.id), raise_on_progress=False
|
||||
)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=bridge.config.name,
|
||||
data={CONF_HOST: bridge.host, CONF_USERNAME: bridge.username},
|
||||
)
|
||||
|
||||
async def async_step_ssdp(self, discovery_info):
|
||||
"""Handle a discovered Hue bridge.
|
||||
@@ -211,3 +233,38 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
self.bridge = self._async_get_bridge(import_info["host"])
|
||||
return await self.async_step_link()
|
||||
|
||||
|
||||
class HueOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle Hue options."""
|
||||
|
||||
def __init__(self, config_entry):
|
||||
"""Initialize Hue options flow."""
|
||||
self.config_entry = config_entry
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Manage Hue options."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(
|
||||
CONF_ALLOW_HUE_GROUPS,
|
||||
default=self.config_entry.options.get(
|
||||
CONF_ALLOW_HUE_GROUPS, False
|
||||
),
|
||||
): bool,
|
||||
vol.Optional(
|
||||
CONF_ALLOW_UNREACHABLE,
|
||||
default=self.config_entry.options.get(
|
||||
CONF_ALLOW_UNREACHABLE, False
|
||||
),
|
||||
): bool,
|
||||
}
|
||||
),
|
||||
)
|
||||
@@ -21,6 +21,6 @@
|
||||
"homekit": {
|
||||
"models": ["BSB002"]
|
||||
},
|
||||
"codeowners": ["@balloob"],
|
||||
"codeowners": ["@balloob", "@frenck"],
|
||||
"quality_scale": "platinum"
|
||||
}
|
||||
@@ -7,6 +7,12 @@
|
||||
"host": "[%key:common::config_flow::data::host%]"
|
||||
}
|
||||
},
|
||||
"manual": {
|
||||
"title": "Manual configure a Hue bridge",
|
||||
"data": {
|
||||
"host": "[%key:common::config_flow::data::host%]"
|
||||
}
|
||||
},
|
||||
"link": {
|
||||
"title": "Link Hub",
|
||||
"description": "Press the button on the bridge to register Philips Hue with Home Assistant.\n\n"
|
||||
@@ -47,5 +53,15 @@
|
||||
"remote_double_button_long_press": "Both \"{subtype}\" released after long press",
|
||||
"remote_double_button_short_press": "Both \"{subtype}\" released"
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"data": {
|
||||
"allow_how_groups": "Allow Hue groups",
|
||||
"allow_unreachable": "Allow unreachable bulbs to report their state correctly"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user