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

View File

@ -37,21 +37,6 @@ from .helpers import (
normalize_hue_transition, 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( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -183,10 +168,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
and flash is None and flash is None
): ):
await self.bridge.async_request_call( await self.bridge.async_request_call(
self.controller.set_state, self.controller.set_state, id=self.resource.id, on=True
id=self.resource.id,
on=True,
allowed_errors=ALLOWED_ERRORS,
) )
return return
@ -202,7 +184,6 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
color_xy=xy_color if light.supports_color else None, color_xy=xy_color if light.supports_color else None,
color_temp=color_temp if light.supports_color_temperature else None, color_temp=color_temp if light.supports_color_temperature else None,
transition_time=transition, transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
) )
for light in self.controller.get_lights(self.resource.id) 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 # To set other features, you'll have to control the attached lights
if transition is None: if transition is None:
await self.bridge.async_request_call( await self.bridge.async_request_call(
self.controller.set_state, self.controller.set_state, id=self.resource.id, on=False
id=self.resource.id,
on=False,
allowed_errors=ALLOWED_ERRORS,
) )
return return
@ -237,7 +215,6 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
light.id, light.id,
on=False, on=False,
transition_time=transition, transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
) )
for light in self.controller.get_lights(self.resource.id) for light in self.controller.get_lights(self.resource.id)
] ]

View File

@ -36,15 +36,6 @@ from .helpers import (
normalize_hue_transition, 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( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -188,7 +179,6 @@ class HueLight(HueBaseEntity, LightEntity):
color_xy=xy_color, color_xy=xy_color,
color_temp=color_temp, color_temp=color_temp,
transition_time=transition, transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
) )
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -208,7 +198,6 @@ class HueLight(HueBaseEntity, LightEntity):
id=self.resource.id, id=self.resource.id,
on=False, on=False,
transition_time=transition, transition_time=transition,
allowed_errors=ALLOWED_ERRORS,
) )
async def async_set_flash(self, flash: str) -> None: 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 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) await task(*args, **kwargs)
bridge.async_request_call = async_request_call bridge.async_request_call = async_request_call