starkillerOG / reolink_aio

Reolink NVR/camera API PyPI package
MIT License
79 stars 16 forks source link

KeyError: 'bSmartTrack' #12

Closed robertklep closed 1 year ago

robertklep commented 1 year ago

Describe the bug The Reolink integration in HA 2023.4.0 logs this error every minute or so (since the root cause seems to be in this library, I posted an issue report here, please let me know if it should be posted to the HA issue tracker instead):

2023-04-06 10:45:21.581 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 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1016, in state
    if (is_on := self.is_on) is None:
  File "/usr/src/homeassistant/homeassistant/components/reolink/switch.py", line 197, in is_on
    return self.entity_description.value(self._host.api, self._channel)
  File "/usr/src/homeassistant/homeassistant/components/reolink/switch.py", line 80, in <lambda>
    value=lambda api, ch: api.auto_track_enabled(ch),
  File "/usr/local/lib/python3.10/site-packages/reolink_aio/api.py", line 2393, in auto_track_enabled
    return self._auto_track_settings[channel]["bSmartTrack"] == 1
KeyError: 'bSmartTrack'

I have three camera's, 2x RLC-523WA and 1x WiFi Doorbell, the issue is related to the latter (which makes sense since it's not a tracking camera).

Environment: Reolink WiFi Doorbell Firmware: v3.0.0.1459_22102806 Hardware: DB_566128M5MP_W

robertklep commented 1 year ago

Looking at the code a bit more, I might be wrong about which camera is causing this.

Only the RLC-523WA's have the GetAiCfg, which is (if I understand correctly) a requisite for auto tracking to be detected. However, both camera's don't have the bSmartTrack key in their JSON (only aiTrack which I guess is the same thing):

   {
      "cmd" : "GetAiCfg",
      "code" : 0,
      "initial" : {
         "AiDetectType" : {
            "dog_cat" : 0,
            "face" : 0,
            "people" : 1,
            "vehicle" : 1
         },
         "aiTrack" : 1,
         "channel" : 0,
         "trackType" : {
            "dog_cat" : 0,
            "face" : 0,
            "people" : 1,
            "vehicle" : 0
         }
      },
      "range" : {
         "AiDetectType" : {
            "dog_cat" : "boolean",
            "face" : "boolean",
            "people" : "boolean",
            "vehicle" : "boolean"
         },
         "aiTrack" : "boolean",
         "channel" : 0,
         "trackType" : {
            "dog_cat" : "boolean",
            "face" : "boolean",
            "people" : "boolean",
            "vehicle" : "boolean"
         }
      },
      "value" : {
         "AiDetectType" : {
            "dog_cat" : 0,
            "face" : 0,
            "people" : 1,
            "vehicle" : 1
         },
         "aiTrack" : 1,
         "channel" : 0,
         "trackType" : {
            "dog_cat" : 0,
            "face" : 0,
            "people" : 1,
            "vehicle" : 0
         }
      }
   },

Device info for both: Reolink RLC-523WA Firmware: v3.1.0.804_22011510_v1.0.0.30 Hardware: IPC_523128M5MP

starkillerOG commented 1 year ago

@robertklep Do you see a switch to toggle on/off AI tracking in the Reolink client/app like the one below: afbeelding

robertklep commented 1 year ago

Yes, I do:

image

(on both RLC-523WA cameras)

If I disable it, aiTrack is set to 0 in the GetAiCfg data.

starkillerOG commented 1 year ago

@robertklep thank you very much for this information, I will implement a fix in the upstream library now

robertklep commented 1 year ago

Thanks!

starkillerOG commented 1 year ago

@robertklep This commit schould fix the issue: https://github.com/starkillerOG/reolink_aio/commit/ecfac30b0974f44eb827270b637d3d86a3134862

I will release a new version of reolink-aio later tonight and bump the HomeAssistant dependency. It will be fixed in a HA patch release in a few days.

starkillerOG commented 1 year ago

@robertklep HA 2023.4.1 is out that schould have the fix, could you try it out and confirm to me if it is fixed?

robertklep commented 1 year ago

@starkillerOG yes apologies for not letting you know, the error has gone 👍🏻