2018-03-30 03:15:40 +00:00
|
|
|
"""Code to handle a Hue bridge."""
|
|
|
|
import asyncio
|
|
|
|
|
2019-04-15 23:45:46 +00:00
|
|
|
import aiohue
|
2018-03-30 03:15:40 +00:00
|
|
|
import async_timeout
|
2019-12-01 05:33:11 +00:00
|
|
|
import slugify as unicode_slug
|
2018-03-30 03:15:40 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
from homeassistant import core
|
2019-02-14 04:36:06 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2018-03-30 03:15:40 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
|
|
|
|
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
2019-10-28 15:45:08 +00:00
|
|
|
from .helpers import create_config_flow
|
2020-01-31 22:47:40 +00:00
|
|
|
from .sensor_base import SensorManager
|
2018-03-30 03:15:40 +00:00
|
|
|
|
|
|
|
SERVICE_HUE_SCENE = "hue_activate_scene"
|
|
|
|
ATTR_GROUP_NAME = "group_name"
|
|
|
|
ATTR_SCENE_NAME = "scene_name"
|
2019-07-31 19:25:30 +00:00
|
|
|
SCENE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required(ATTR_GROUP_NAME): cv.string, vol.Required(ATTR_SCENE_NAME): cv.string}
|
|
|
|
)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class HueBridge:
|
2018-03-30 03:15:40 +00:00
|
|
|
"""Manages a single Hue bridge."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config_entry, allow_unreachable, allow_groups):
|
|
|
|
"""Initialize the system."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.hass = hass
|
|
|
|
self.allow_unreachable = allow_unreachable
|
|
|
|
self.allow_groups = allow_groups
|
|
|
|
self.available = True
|
2019-10-28 15:45:08 +00:00
|
|
|
self.authorized = False
|
2018-03-30 03:15:40 +00:00
|
|
|
self.api = None
|
2019-12-01 21:24:16 +00:00
|
|
|
self.parallel_updates_semaphore = None
|
2020-01-31 22:47:40 +00:00
|
|
|
# Jobs to be executed when API is reset.
|
|
|
|
self.reset_jobs = []
|
|
|
|
self.sensor_manager = None
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2018-04-01 16:03:01 +00:00
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""Return the host of this bridge."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.config_entry.data["host"]
|
2018-04-01 16:03:01 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def async_setup(self, tries=0):
|
|
|
|
"""Set up a phue bridge based on host parameter."""
|
2018-04-01 16:03:01 +00:00
|
|
|
host = self.host
|
2018-04-09 14:09:08 +00:00
|
|
|
hass = self.hass
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
bridge = aiohue.Bridge(
|
|
|
|
host,
|
|
|
|
username=self.config_entry.data["username"],
|
|
|
|
websession=aiohttp_client.async_get_clientsession(hass),
|
|
|
|
)
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
try:
|
2019-12-16 18:45:09 +00:00
|
|
|
await authenticate_bridge(hass, bridge)
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
except AuthenticationRequired:
|
2019-02-14 15:01:46 +00:00
|
|
|
# Usernames can become invalid if hub is reset or user removed.
|
2018-03-30 03:15:40 +00:00
|
|
|
# We are going to fail the config entry setup and initiate a new
|
|
|
|
# linking procedure. When linking succeeds, it will remove the
|
|
|
|
# old config entry.
|
2019-10-28 15:45:08 +00:00
|
|
|
create_config_flow(hass, host)
|
2018-03-30 03:15:40 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
except CannotConnect:
|
2019-05-22 12:59:16 +00:00
|
|
|
LOGGER.error("Error connecting to the Hue bridge at %s", host)
|
2019-02-14 04:36:06 +00:00
|
|
|
raise ConfigEntryNotReady
|
2018-03-30 03:15:40 +00:00
|
|
|
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.exception("Unknown error connecting with Hue bridge at %s", host)
|
2018-03-30 03:15:40 +00:00
|
|
|
return False
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
self.api = bridge
|
2020-01-31 22:47:40 +00:00
|
|
|
self.sensor_manager = SensorManager(self)
|
2019-12-16 18:45:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(self.config_entry, "light")
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
|
|
|
self.config_entry, "binary_sensor"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(self.config_entry, "sensor")
|
|
|
|
)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2018-04-09 14:09:08 +00:00
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, SERVICE_HUE_SCENE, self.hue_activate_scene, schema=SCENE_SCHEMA
|
|
|
|
)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2019-12-01 21:24:16 +00:00
|
|
|
self.parallel_updates_semaphore = asyncio.Semaphore(
|
|
|
|
3 if self.api.config.modelid == "BSB001" else 10
|
|
|
|
)
|
|
|
|
|
2019-10-28 15:45:08 +00:00
|
|
|
self.authorized = True
|
2018-03-30 03:15:40 +00:00
|
|
|
return True
|
|
|
|
|
2019-12-01 21:24:16 +00:00
|
|
|
async def async_request_call(self, coro):
|
|
|
|
"""Process request batched."""
|
|
|
|
|
|
|
|
async with self.parallel_updates_semaphore:
|
|
|
|
return await coro
|
|
|
|
|
2018-04-12 12:28:54 +00:00
|
|
|
async def async_reset(self):
|
|
|
|
"""Reset this bridge to default state.
|
|
|
|
|
|
|
|
Will cancel any scheduled setup retry and will unload
|
|
|
|
the config entry.
|
|
|
|
"""
|
|
|
|
# The bridge can be in 3 states:
|
|
|
|
# - Setup was successful, self.api is not None
|
|
|
|
# - Authentication was wrong, self.api is None, not retrying setup.
|
|
|
|
|
|
|
|
# If the authentication was wrong.
|
|
|
|
if self.api is None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
self.hass.services.async_remove(DOMAIN, SERVICE_HUE_SCENE)
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
while self.reset_jobs:
|
|
|
|
self.reset_jobs.pop()()
|
|
|
|
|
2018-04-12 12:28:54 +00:00
|
|
|
# If setup was successful, we set api variable, forwarded entry and
|
|
|
|
# register service
|
2019-04-18 05:13:03 +00:00
|
|
|
results = await asyncio.gather(
|
|
|
|
self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, "light"
|
|
|
|
),
|
2019-04-18 05:13:03 +00:00
|
|
|
self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, "binary_sensor"
|
|
|
|
),
|
2019-04-18 05:13:03 +00:00
|
|
|
self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, "sensor"
|
|
|
|
),
|
|
|
|
)
|
2020-01-31 22:47:40 +00:00
|
|
|
|
2019-04-18 05:13:03 +00:00
|
|
|
# None and True are OK
|
|
|
|
return False not in results
|
2018-04-12 12:28:54 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def hue_activate_scene(self, call, updated=False):
|
|
|
|
"""Service to call directly into bridge to set scenes."""
|
|
|
|
group_name = call.data[ATTR_GROUP_NAME]
|
|
|
|
scene_name = call.data[ATTR_SCENE_NAME]
|
|
|
|
|
|
|
|
group = next(
|
2019-07-31 19:25:30 +00:00
|
|
|
(group for group in self.api.groups.values() if group.name == group_name),
|
|
|
|
None,
|
|
|
|
)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2018-06-27 19:22:29 +00:00
|
|
|
# Additional scene logic to handle duplicate scene names across groups
|
|
|
|
scene = next(
|
2019-07-31 19:25:30 +00:00
|
|
|
(
|
|
|
|
scene
|
|
|
|
for scene in self.api.scenes.values()
|
|
|
|
if scene.name == scene_name
|
|
|
|
and group is not None
|
|
|
|
and sorted(scene.lights) == sorted(group.lights)
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
|
|
|
# If we can't find it, fetch latest info.
|
2018-06-27 19:22:29 +00:00
|
|
|
if not updated and (group is None or scene is None):
|
2018-03-30 03:15:40 +00:00
|
|
|
await self.api.groups.update()
|
|
|
|
await self.api.scenes.update()
|
|
|
|
await self.hue_activate_scene(call, updated=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
if group is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.warning("Unable to find group %s", group_name)
|
2018-03-30 03:15:40 +00:00
|
|
|
return
|
|
|
|
|
2018-06-27 19:22:29 +00:00
|
|
|
if scene is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.warning("Unable to find scene %s", scene_name)
|
2018-03-30 03:15:40 +00:00
|
|
|
return
|
|
|
|
|
2018-06-27 19:22:29 +00:00
|
|
|
await group.set_action(scene=scene.id)
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2019-10-28 15:45:08 +00:00
|
|
|
async def handle_unauthorized_error(self):
|
|
|
|
"""Create a new config flow when the authorization is no longer valid."""
|
|
|
|
if not self.authorized:
|
|
|
|
# we already created a new config flow, no need to do it again
|
|
|
|
return
|
|
|
|
LOGGER.error(
|
|
|
|
"Unable to authorize to bridge %s, setup the linking again.", self.host
|
|
|
|
)
|
|
|
|
self.authorized = False
|
|
|
|
create_config_flow(self.hass, self.host)
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def authenticate_bridge(hass: core.HomeAssistant, bridge: aiohue.Bridge):
|
2018-03-30 03:15:40 +00:00
|
|
|
"""Create a bridge object and verify authentication."""
|
|
|
|
try:
|
2019-05-24 22:55:13 +00:00
|
|
|
with async_timeout.timeout(10):
|
2018-03-30 03:15:40 +00:00
|
|
|
# Create username if we don't have one
|
2019-12-16 18:45:09 +00:00
|
|
|
if not bridge.username:
|
2019-12-01 05:33:11 +00:00
|
|
|
device_name = unicode_slug.slugify(
|
|
|
|
hass.config.location_name, max_length=19
|
|
|
|
)
|
|
|
|
await bridge.create_user(f"home-assistant#{device_name}")
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
# Initialize bridge (and validate our username)
|
|
|
|
await bridge.initialize()
|
|
|
|
|
|
|
|
except (aiohue.LinkButtonNotPressed, aiohue.Unauthorized):
|
|
|
|
raise AuthenticationRequired
|
|
|
|
except (asyncio.TimeoutError, aiohue.RequestError):
|
|
|
|
raise CannotConnect
|
|
|
|
except aiohue.AiohueException:
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.exception("Unknown Hue linking error occurred")
|
2018-03-30 03:15:40 +00:00
|
|
|
raise AuthenticationRequired
|