Skip to content

Commit

Permalink
Gracefully mapping value to lock_state (kvj#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
inc-ali committed Jan 3, 2024
1 parent f5e61bd commit 052fe46
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/nuki_ng/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def state(self):
return str(self.get_state())

def get_state(self) -> DoorSecurityStates:
lock_state = LockStates(self.last_state.get("state"))
lock_state = LockStates(self.last_state.get("state", LockStates.UNDEFINED.value))
door_sensor_state = DoorSensorStates(
self.last_state.get("doorsensorState"))

Expand Down

1 comment on commit 052fe46

@poughkeepsee
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @inc-ali

I tired implementing this fix manually, this solved my error from kvj#168 but sparked a new error in my config:

2024-01-10 12:20:07.301 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 243, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 399, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 182, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 479, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 941, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1062, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 999, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 947, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/config/custom_components/nuki_ng/sensor.py", line 143, in state
    return str(self.get_state())
               ^^^^^^^^^^^^^^^^
  File "/config/custom_components/nuki_ng/sensor.py", line 147, in get_state
    door_sensor_state = DoorSensorStates(
                        ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/enum.py", line 712, in __call__
    return cls.__new__(cls, value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/enum.py", line 1135, in __new__
    raise ve_exc
ValueError: None is not a valid DoorSensorStates

I then tried following the same reasoning as the fix above and edit my config to:

    def get_state(self) -> DoorSecurityStates:
        lock_state = LockStates(self.last_state.get("state", LockStates.UNDEFINED.value))
        door_sensor_state = DoorSensorStates(
            self.last_state.get("doorsensorState", DoorSensorStates.UNDEFINED.value))

But it didn't work, got yet another error:

2024-01-10 13:38:18.639 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up nuki_ng platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 368, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 672, in _async_add_entity
    original_icon=entity.icon,
                  ^^^^^^^^^^^
  File "/config/custom_components/nuki_ng/sensor.py", line 133, in icon
    state = self.get_state()
            ^^^^^^^^^^^^^^^^
  File "/config/custom_components/nuki_ng/sensor.py", line 148, in get_state
    self.last_state.get("doorsensorState", DoorSensorStates.UNDEFINED.value))
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/enum.py", line 784, in __getattr__
    raise AttributeError(name) from None
AttributeError: UNDEFINED

Any advice on a possible fix for the new None is not a valid DoorSensorStates error?

Thanks!

Please sign in to comment.