1 """Code to handle a Hue bridge."""
3 from __future__
import annotations
6 from collections.abc
import Callable
11 from aiohttp
import client_exceptions
12 from aiohue
import HueBridgeV1, HueBridgeV2, LinkButtonNotPressed, Unauthorized
13 from aiohue.errors
import AiohueException, BridgeBusy
15 from homeassistant
import core
21 from .const
import DOMAIN
22 from .v1.sensor_base
import SensorManager
23 from .v2.device
import async_setup_devices
24 from .v2.hue_event
import async_setup_hue_events
29 PLATFORMS_v1 = [Platform.BINARY_SENSOR, Platform.LIGHT, Platform.SENSOR]
31 Platform.BINARY_SENSOR,
41 """Manages a single Hue bridge."""
43 def __init__(self, hass: core.HomeAssistant, config_entry: ConfigEntry) ->
None:
44 """Initialize the system."""
49 self.reset_jobs: list[core.CALLBACK_TYPE] = []
51 self.
loggerlogger = logging.getLogger(__name__)
53 app_key: str = self.
config_entryconfig_entry.data[CONF_API_KEY]
55 self.
apiapi = HueBridgeV1(
56 self.
hosthost, app_key, aiohttp_client.async_get_clientsession(hass)
59 self.
apiapi = HueBridgeV2(self.
hosthost, app_key)
61 hass.data.setdefault(DOMAIN, {})[self.
config_entryconfig_entry.entry_id] = self
65 """Return the host of this bridge."""
70 """Return api version we're set-up for."""
71 return self.
config_entryconfig_entry.data[CONF_API_VERSION]
74 """Initialize Connection with the Hue API."""
77 async
with asyncio.timeout(10):
78 await self.
apiapi.initialize()
80 except (LinkButtonNotPressed, Unauthorized):
89 client_exceptions.ClientOSError,
90 client_exceptions.ServerDisconnectedError,
91 client_exceptions.ContentTypeError,
95 f
"Error connecting to the Hue bridge at {self.host}"
98 self.
loggerlogger.exception(
"Unknown error connecting to Hue bridge")
102 await self.
apiapi.close()
106 if self.
apiapi.sensors
is not None:
108 await self.
hasshass.config_entries.async_forward_entry_setups(
116 await self.
hasshass.config_entries.async_forward_entry_setups(
121 self.reset_jobs.append(self.
config_entryconfig_entry.add_update_listener(_update_listener))
126 """Send request to the Hue bridge."""
128 return await task(*args, **kwargs)
129 except AiohueException
as err:
132 msg = f
"Request failed: {err}"
133 if "may not have effect" in str(err):
135 self.
loggerlogger.debug(msg)
138 except aiohttp.ClientError
as err:
140 f
"Request failed due connection error: {err}"
144 """Reset this bridge to default state.
146 Will cancel any scheduled setup retry and will unload
154 if self.
apiapi
is None:
157 while self.reset_jobs:
158 self.reset_jobs.pop()()
161 unload_success = await self.
hasshass.config_entries.async_unload_platforms(
168 return unload_success
171 """Create a new config flow when the authorization is no longer valid."""
176 "Unable to authorize to bridge %s, setup the linking again", self.
hosthost
183 """Handle ConfigEntry options update."""
184 await hass.config_entries.async_reload(entry.entry_id)
188 """Start a config flow."""
189 hass.async_create_task(
190 hass.config_entries.flow.async_init(
192 context={
"source": SOURCE_IMPORT},
Any async_request_call(self, Callable task, *args, **kwargs)
bool async_initialize_bridge(self)
None handle_unauthorized_error(self)
None __init__(self, core.HomeAssistant hass, ConfigEntry config_entry)
None create_config_flow(core.HomeAssistant hass, str host)
None _update_listener(core.HomeAssistant hass, ConfigEntry entry)
def async_setup_devices(HueBridge bridge)
def async_setup_hue_events(HueBridge bridge)