spacemanspiff2007 / HABApp

Easy home automation with MQTT and/or openHAB
Apache License 2.0
54 stars 23 forks source link

missing check for None in Rollershutter Item #331

Closed UrsusS closed 1 year ago

UrsusS commented 1 year ago

HABApp 1.0.4 openHAB 3.4.0.M2

A None-value of a rollershutter item leads to a TypeError in is_down().

2022-10-13 06:00:02.012 [ERROR] [HABApp.Worker   ] - File "/etc/openhab/habapp/rules/lawnmower_shed.py", line 101 in Close
2022-10-13 06:00:02.017 [ERROR] [HABApp.Worker   ] - --------------------------------------------------------------------------------
2022-10-13 06:00:02.017 [ERROR] [HABApp.Worker   ] -       91 | def Close(self):
2022-10-13 06:00:02.020 [ERROR] [HABApp.Worker   ] -        (...)
2022-10-13 06:00:02.021 [ERROR] [HABApp.Worker   ] -       97 |     # HAND-Betrieb?
2022-10-13 06:00:02.022 [ERROR] [HABApp.Worker   ] -       98 |     ManMode = self.isManMode()
2022-10-13 06:00:02.023 [ERROR] [HABApp.Worker   ] -      100 |     ShutterItem = RollershutterItem.get_item("rol_MowerShed_Gate")
2022-10-13 06:00:02.023 [ERROR] [HABApp.Worker   ] - -->  101 |     if not ManMode and not ShutterItem.is_down():
2022-10-13 06:00:02.026 [ERROR] [HABApp.Worker   ] -      102 |         ShutterItem.down()
2022-10-13 06:00:02.030 [ERROR] [HABApp.Worker   ] -    ------------------------------------------------------------
2022-10-13 06:00:02.031 [ERROR] [HABApp.Worker   ] -      self = <LawnmowerShed>
2022-10-13 06:00:02.031 [ERROR] [HABApp.Worker   ] -      self.logMower = <Logger Mower (DEBUG)>
2022-10-13 06:00:02.032 [ERROR] [HABApp.Worker   ] -      LOGGER = 'LawnmowerShed.Close '
2022-10-13 06:00:02.032 [ERROR] [HABApp.Worker   ] -      LOGGER + "Rolltor wird geschlossen" = 'LawnmowerShed.Close Rolltor wird geschlossen'
2022-10-13 06:00:02.032 [ERROR] [HABApp.Worker   ] -      ManMode = False
2022-10-13 06:00:02.033 [ERROR] [HABApp.Worker   ] -      not ManMode = True
2022-10-13 06:00:02.033 [ERROR] [HABApp.Worker   ] -      ShutterItem = <RollershutterItem name: rol_MowerShed_Gate, value: None, last_change: 2022-10-09T13:13:24.870327, last_update: 2022-10-09T13:13:24.870327>
2022-10-13 06:00:02.033 [ERROR] [HABApp.Worker   ] -    ------------------------------------------------------------
2022-10-13 06:00:02.034 [ERROR] [HABApp.Worker   ] - 
2022-10-13 06:00:02.034 [ERROR] [HABApp.Worker   ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/items/rollershutter_item.py", line 42 in is_down
2022-10-13 06:00:02.034 [ERROR] [HABApp.Worker   ] - --------------------------------------------------------------------------------
2022-10-13 06:00:02.035 [ERROR] [HABApp.Worker   ] -      41 | def is_down(self) -> bool:
2022-10-13 06:00:02.035 [ERROR] [HABApp.Worker   ] - -->  42 |     return self.value >= 100
2022-10-13 06:00:02.035 [ERROR] [HABApp.Worker   ] -    ------------------------------------------------------------
2022-10-13 06:00:02.036 [ERROR] [HABApp.Worker   ] -      self = <RollershutterItem name: rol_MowerShed_Gate, value: None, last_change: 2022-10-09T13:13:24.870327, last_update: 2022-10-09T13:13:24.870327>
2022-10-13 06:00:02.036 [ERROR] [HABApp.Worker   ] -      self.value = None
2022-10-13 06:00:02.036 [ERROR] [HABApp.Worker   ] -    ------------------------------------------------------------
2022-10-13 06:00:02.037 [ERROR] [HABApp.Worker   ] - 
2022-10-13 06:00:02.037 [ERROR] [HABApp.Worker   ] - --------------------------------------------------------------------------------
2022-10-13 06:00:02.037 [ERROR] [HABApp.Worker   ] - Traceback (most recent call last):
2022-10-13 06:00:02.038 [ERROR] [HABApp.Worker   ] -   File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/internals/wrapped_function/wrapped_thread.py", line 79, in run_sync
2022-10-13 06:00:02.038 [ERROR] [HABApp.Worker   ] -     self.func(*args, **kwargs)
2022-10-13 06:00:02.038 [ERROR] [HABApp.Worker   ] -   File "/etc/openhab/habapp/rules/lawnmower.py", line 920, in DoorOpenClose
2022-10-13 06:00:02.039 [ERROR] [HABApp.Worker   ] -     Shed.Close()
2022-10-13 06:00:02.039 [ERROR] [HABApp.Worker   ] -   File "/etc/openhab/habapp/rules/lawnmower_shed.py", line 101, in Close
2022-10-13 06:00:02.039 [ERROR] [HABApp.Worker   ] -     if not ManMode and not ShutterItem.is_down():
2022-10-13 06:00:02.040 [ERROR] [HABApp.Worker   ] -   File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/items/rollershutter_item.py", line 42, in is_down
2022-10-13 06:00:02.040 [ERROR] [HABApp.Worker   ] -     return self.value >= 100
2022-10-13 06:00:02.041 [ERROR] [HABApp.Worker   ] - TypeError: '>=' not supported between instances of 'NoneType' and 'int'

I would expect that the code handles None values. May be something like this (not tested, just typed):

def is_down(self) -> bool:
    return self.value is not None and self.value >= 100
spacemanspiff2007 commented 1 year ago

The item value is None if there has been no value received from the device (e.g. after a restart). If you restart your openHAB instance at night your suggestion would return false even though the shutter would actually be closed.

I'd rather log an error than return a possibly wrong value. You can initialize the state on the HABApp side with set_value() or post an update to the openHAB item when the rule gets initialized. e.g.


def __init__():
    ...
    self.run.soon(self.initialize)

def initialize(self):
    if self.ShutterItem.value is not None:
        # Chose one, depending on what you want to initialize
        self.ShutterItem.post_value(0)
        self.ShutterItem.oh_post_value(0)

    # from the next release on
    self.ShutterItem.post_value_if(0, is_=None)
    self.ShutterItem.oh_post_value_if(0, is_=None)
UrsusS commented 1 year ago

Thanks for the hints. I see I have to solve it on application level as on library level it is not clear what the right reaction would be.