jcgoette / baby_buddy_homeassistant

This custom integration provides sensors for Baby Buddy API endpoints.
MIT License
59 stars 26 forks source link

add_feeding throwing KeyError #106

Closed droans closed 1 year ago

droans commented 1 year ago

Calling babybuddy.add_feeding throws a KeyError with missing key 'wet'. BabyBuddy does update, though.

After calling the service, it appears to retry every sixty seconds but returns the same error.

Service call:

service: babybuddy.add_feeding
data:
  type: Solid food
  method: Bottle
  start: "00:00:00"
  end: "00:10:00"
  amount: 3
target:
  device_id: 2dc385be0a07f6f9c8419f37411dd117

Error Log from service call:

2023-04-27 15:14:41.797 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140504071958640] Error handling message: Unknown error (unknown_error) Michael from 108.162.237.68 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 647, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1533, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 410, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 454, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 477, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 452, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 685, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1820, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1857, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 809, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 798, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 977, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 838, in _handle_entity_call
    await result
  File "/config/custom_components/babybuddy/switch.py", line 228, in async_add_feeding
    await self.coordinator.async_request_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 200, in async_request_refresh
    await self._debounced_refresh.async_call()
  File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 89, in async_call
    await task
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 226, in async_refresh
    await self._async_refresh(log_failures=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 341, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 401, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 635, in _async_write_ha_state
    attr.update(self.extra_state_attributes or {})
  File "/config/custom_components/babybuddy/sensor.py", line 511, in extra_state_attributes
    self.coordinator.data[1][self.child[ATTR_ID]][
KeyError: 'wet'

Error log every sixty seconds after:

2023-04-27 15:18:41.340 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 193, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 341, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 401, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 635, in _async_write_ha_state
    attr.update(self.extra_state_attributes or {})
  File "/config/custom_components/babybuddy/sensor.py", line 511, in extra_state_attributes
    self.coordinator.data[1][self.child[ATTR_ID]][
KeyError: 'wet'
jcgoette commented 1 year ago

I don't think this is d/t the add_feeding service call.

I am guessing you have diaper entries, but none marked as Wet?

droans commented 1 year ago

Nope, this was from calling babybuddy.add_feeding.

Once I added a wet diaper change, it did stop complaining. However, it still will not update the last feeding sensor. Even removing and readding the integration doesn't fix it.

image

Here is a log from the most recent time the error occurred:


2023-05-11 11:20:01.535 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.teddy_last_feeding fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 554, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 444, in async_update
    await self.coordinator.async_request_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 211, in async_request_refresh
    await self._debounced_refresh.async_call()
  File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 95, in async_call
    await task
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 237, in async_refresh
    await self._async_refresh(log_failures=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 349, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 146, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 406, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 585, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 652, in _async_write_ha_state
    attr.update(self.extra_state_attributes or {})
  File "/config/custom_components/babybuddy/sensor.py", line 511, in extra_state_attributes
KeyError: 'wet'

Edit: Ah, last feeding uses the amount for the state instead of the time like the sensor for the diaper change. Attributes are still reported correctly.

jcgoette commented 1 year ago

Should be fixed by https://github.com/jcgoette/baby_buddy_homeassistant/commit/4c146e1d921efa7400d091525c0fa3f4ef0042bf.