Simplify Hue error handling a bit (#68529)

This commit is contained in:
Marcel van der Veldt 2022-03-23 05:59:06 +01:00 committed by GitHub
parent 7381c2114f
commit dd1d7fdbab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 48 deletions

View File

@ -117,22 +117,19 @@ class HueBridge:
self.authorized = True
return True
async def async_request_call(
self, task: Callable, *args, allowed_errors: list[str] | None = None, **kwargs
) -> Any:
"""Send request to the Hue bridge, optionally omitting error(s)."""
async def async_request_call(self, task: Callable, *args, **kwargs) -> Any:
"""Send request to the Hue bridge."""
try:
return await task(*args, **kwargs)
except AiohueException as err:
# The (new) Hue api can be a bit fanatic with throwing errors
# some of which we accept in certain conditions
# handle that here. Note that these errors are strings and do not have
# an identifier or something.
if allowed_errors is not None and str(err) in allowed_errors:
# The (new) Hue api can be a bit fanatic with throwing errors so
# we have some logic to treat some responses as warning only.
msg = f"Request failed: {err}"
if "may not have effect" in str(err):
# log only
self.logger.debug("Ignored error/warning from Hue API: %s", str(err))
self.logger.debug(msg)
return None
raise HomeAssistantError(f"Request failed: {err}") from err
raise HomeAssistantError(msg) from err
except aiohttp.ClientError as err:
raise HomeAssistantError(
f"Request failed due connection error: {err}"

View File

@ -37,21 +37,6 @@ from .helpers import (
normalize_hue_transition,
)
ALLOWED_ERRORS = [
"device (groupedLight) has communication issues, command (on) may not have effect",
'device (groupedLight) is "soft off", command (on) may not have effect',
"device (light) has communication issues, command (on) may not have effect",
'device (light) is "soft off", command (on) may not have effect',
"device (grouped_light) has communication issues, command (.on) may not have effect",
'device (grouped_light) is "soft off", command (.on) may not have effect'
"device (grouped_light) has communication issues, command (.on.on) may not have effect",
'device (grouped_light) is "soft off", command (.on.on) may not have effect'
"device (light) has communication issues, command (.on) may not have effect",
'device (light) is "soft off", command (.on) may not have effect',
"device (light) has communication issues, command (.on.on) may not have effect",
'device (light) is "soft off", command (.on.on) may not have effect',
]
async def async_setup_entry(
hass: HomeAssistant,
@ -183,10 +168,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
and flash is None
):
await self.bridge.async_request_call(
self.controller.set_state,
id=self.resource.id,
on=True,
allowed_errors=ALLOWED_ERRORS,
self.controller.set_state, id=self.resource.id, on=True
)
return
@ -202,7 +184,6 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
color_xy=xy_color if light.supports_color else None,
color_temp=color_temp if light.supports_color_temperature else None,
transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
)
for light in self.controller.get_lights(self.resource.id)
]
@ -222,10 +203,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
# To set other features, you'll have to control the attached lights
if transition is None:
await self.bridge.async_request_call(
self.controller.set_state,
id=self.resource.id,
on=False,
allowed_errors=ALLOWED_ERRORS,
self.controller.set_state, id=self.resource.id, on=False
)
return
@ -237,7 +215,6 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
light.id,
on=False,
transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
)
for light in self.controller.get_lights(self.resource.id)
]

View File

@ -36,15 +36,6 @@ from .helpers import (
normalize_hue_transition,
)
ALLOWED_ERRORS = [
"device (light) has communication issues, command (on) may not have effect",
'device (light) is "soft off", command (on) may not have effect',
"device (light) has communication issues, command (.on) may not have effect",
'device (light) is "soft off", command (.on) may not have effect',
"device (light) has communication issues, command (.on.on) may not have effect",
'device (light) is "soft off", command (.on.on) may not have effect',
]
async def async_setup_entry(
hass: HomeAssistant,
@ -188,7 +179,6 @@ class HueLight(HueBaseEntity, LightEntity):
color_xy=xy_color,
color_temp=color_temp,
transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
)
async def async_turn_off(self, **kwargs: Any) -> None:
@ -208,7 +198,6 @@ class HueLight(HueBaseEntity, LightEntity):
id=self.resource.id,
on=False,
transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
)
async def async_set_flash(self, flash: str) -> None:

View File

@ -60,7 +60,7 @@ def create_mock_bridge(hass, api_version=1):
bridge.async_initialize_bridge = async_initialize_bridge
async def async_request_call(task, *args, allowed_errors=None, **kwargs):
async def async_request_call(task, *args, **kwargs):
await task(*args, **kwargs)
bridge.async_request_call = async_request_call