adaptive-lighting icon indicating copy to clipboard operation
adaptive-lighting copied to clipboard

Unexpected error for call_service at pos 1: 'brightness_pct'

Open bverkron opened this issue 4 years ago • 2 comments

Version information:

1.0.15

Description:

Sometime within the last 1 week the adaptive lighting effects stopped working. If a light has had it's color changed toggling it off/on will no longer return control to adaptive lighting it stays on the custom color. I see nothing in the logs when performing the off/on operation to indicate why it's not returning control.

I tried:

  • Restarting HA
  • Reloading and disabling / re-enabling Adaptive Lighting
  • Deleting the instance of Adaptive Lighting and re-creating it
  • Completely removing the Adaptive Lighting integration via HACS, restarting HA, re-installing via HACS
image image

Nothing has helped. When I manually call the Adaptive Lighting apply service ...

service: adaptive_lighting.apply
data:
  entity_id: switch.adaptive_lighting_home
  lights: light.living_room_lamp_bulb
  adapt_color: 'yes'

... I get these errors in the HA log ...

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/adaptive_lighting/switch.py:795 
Integration: Home Assistant WebSocket API (documentation, issues) 
First occurred: 10:48:04 AM (1 occurrences) 
Last logged: 10:48:04 AM

[548126124928] Error handling message: Unknown error
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 527, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1260, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 363, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 381, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 584, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1495, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1530, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 691, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 663, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 896, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 700, in _handle_entity_call
    await result
  File "/config/custom_components/adaptive_lighting/switch.py", line 236, in handle_apply
    await switch._adapt_light(  # pylint: disable=protected-access
  File "/config/custom_components/adaptive_lighting/switch.py", line 795, in _adapt_light
    brightness = round(255 * self._settings["brightness_pct"] / 100)
KeyError: 'brightness_pct'
Logger: homeassistant.helpers.script.websocket_api_script
Source: custom_components/adaptive_lighting/switch.py:795 
Integration: Adaptive Lighting (documentation, issues) 
First occurred: 10:48:04 AM (1 occurrences) 
Last logged: 10:48:04 AM

websocket_api script: Error executing script. Unexpected error for call_service at pos 1: 'brightness_pct'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 381, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 584, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1495, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1530, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 691, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 663, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 896, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 700, in _handle_entity_call
    await result
  File "/config/custom_components/adaptive_lighting/switch.py", line 236, in handle_apply
    await switch._adapt_light(  # pylint: disable=protected-access
  File "/config/custom_components/adaptive_lighting/switch.py", line 795, in _adapt_light
    brightness = round(255 * self._settings["brightness_pct"] / 100)
KeyError: 'brightness_pct'

I don't recall doing any HA updates before it broke but I did update to the current latest version of HA (2021.12.10) after it stopped working in the hopes it would fix it, it did not. I was on 2021.12.2 before the update IIRC.

bverkron avatar Jan 22 '22 18:01 bverkron

Found a "solution". Somehow the adaptive lighting switch got turned off and was still turned off after completely re-installing Adaptive Lighting. The switch being off seems to be the cause of the error as noted here:

Let's assume we have set up adaptive lighting with a name of "Big Lights". You would have a switch like so: switch.adaptive_lighting_big_lights... if switch.adaptive_lighting_big_lights is switched off, then calling the service ... will hit the above mentioned error. The reason is that "self._settings" is simply an empty dict at that point because it's never been initialised.

https://github.com/basnijholt/adaptive-lighting/issues/119#issuecomment-940150738

It would be best to handle this gracefully and at minimum enter something into the log that clearly indicates the switch is off so adaptive lighting cannot be applied, instead of an obscure unhandled error like this.

bverkron avatar Jan 22 '22 19:01 bverkron

Thanks, this was my issue too.

dekiesel avatar Feb 24 '22 19:02 dekiesel

This problem is solved by https://github.com/basnijholt/adaptive-lighting/pull/326.

Please update to 1.0.20! 😄

@bverkron, your reply made me understand the root cause. Thanks a lot!

basnijholt avatar Aug 30 '22 17:08 basnijholt

Great!

bverkron avatar Aug 30 '22 18:08 bverkron