spacemanspiff2007 / HABApp

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

OH 4 reports datetime itemvalues with nanoseconds #395

Closed dobernhardt closed 1 year ago

dobernhardt commented 1 year ago

OH 4.0.0.M3 seems to report datetime itemvalues with nanoseconds precision which fails parsing to a python DateTime object. Apparently %f works only up to microseconds.

Exception in on_sse_event: time data '2023-06-17T15:31:04.754673068+0200' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
    File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/connection_handler/sse_handler.py", line 32 in on_sse_event
    --------------------------------------------------------------------------------
         29 | def on_sse_event(event_dict: dict, oh_3: bool):
         30 |     try:
         31 |         # Lookup corresponding OpenHAB event
    -->  32 |         event = get_event(event_dict)
         34 |         # Update item in registry BEFORE posting to the event bus
         35 |         # so the items have the correct state when we process the event in a rule
       ------------------------------------------------------------
         (ItemAddedEvent, ItemUpdatedEvent) = (<class 'HABApp.openhab.events.item_events.ItemAddedEvent'>, <class 'HABApp.openhab.events.item_events.ItemUpdatedEvent'>)
         (ThingStatusInfoEvent, ThingUpdatedEvent, ThingConfigStatusInfoEvent) = (<class 'HABApp.openhab.events.thing_events.ThingStatusInfoEvent'>, <class 'HABApp.openhab.events.thing_events.ThingUpdatedEvent'>, <class 'HABApp.openhab.events.thing_events.ThingConfigStatusInfoEvent'>)
         e = ValueError("time data '2023-06-17T15:31:04.754673068+0200' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'")
         event_dict = {'topic': 'openhab/items/weather_sensor_netatmo_rain_last_message/statechanged', 'payload': '{"type":"DateTime","value":"2023-06-17T15:31:04.754673068+0200","oldType":"DateTime","oldValue":"2023-06-17T15:31:04.734500815+0200"}', 'type': 'ItemStateChangedEvent'}
         log = <Logger HABApp.openhab.connection (INFO)>
         oh_3 = False
         oh_3 and isinstance(event, GroupStateChangedEvent) = False
         TOPIC_ITEMS = 'openHAB.Items'
         TOPIC_THINGS = 'openHAB.Things'
       ------------------------------------------------------------

    File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/map_events.py", line 44 in get_event
    --------------------------------------------------------------------------------
         37 | def get_event(_in_dict: dict) -> OpenhabEvent:
          (...)
         40 |     payload: dict = load_json(_in_dict['payload'])
         42 |     # Find event from implemented events
         43 |     try:
    -->  44 |         return _events[event_type].from_dict(topic, payload)
         45 |     except KeyError:
       ------------------------------------------------------------
         _events[event_type] = <class 'HABApp.openhab.events.item_events.ItemStateChangedEvent'>
         _events = {'ItemStateEvent': <class 'HABApp.openhab.events.item_events.ItemStateEvent'>, 'ItemStateUpdatedEvent': <class 'HABApp.openhab.events.item_events.ItemStateUpdatedEvent'>, 'ItemStateChangedEvent': <class 'HABApp.openhab.events.item_events.ItemStateChangedEvent'>, 'ItemCommandEvent': <class 'HABApp.openhab.events.item_events.ItemCommandEvent'>, 'ItemAddedEvent': <class 'HABApp.openhab.events.item_events.ItemAddedEvent'>, 'ItemUpdatedEvent': <class 'HABApp.openhab.events.item_events.ItemUpdatedEvent'>, 'ItemRemovedEvent': <class 'HABApp.openhab.events.item_events.ItemRemovedEvent'>, 'ItemStatePredictedEvent': <class 'HABApp.openhab.events.item_events.ItemStatePredictedEvent'>, 'GroupStateUpdatedEvent': <class 'HABApp.openhab.events.item_events.GroupStateUpdatedEvent'>, 'GroupStateChangedEvent': <class 'HABApp.openhab.events.item_events.GroupStateChangedEvent'>, 'ChannelTriggeredEvent': <class 'HABApp.openhab.events.channel_events.ChannelTriggeredEvent'>, 'ChannelDescriptionChangedEvent': <class 'HABApp.openhab.events.channel_events.ChannelDescriptionChangedEvent'>, 'ThingAddedEvent': <class 'HABApp.openhab.events.thing_events.ThingAddedEvent'>, 'ThingRemovedEvent': <class 'HABApp.openhab.events.thing_events.ThingRemovedEvent'>, 'ThingUpdatedEvent': <class 'HABApp.openhab.events.thing_events.ThingUpdatedEvent'>, 'ThingStatusInfoEvent': <class 'HABApp.openhab.events.thing_events.ThingStatusInfoEvent'>, 'ThingStatusInfoChangedEvent': <class 'HABApp.openhab.events.thing_events.ThingStatusInfoChangedEvent'>, 'ThingFirmwareStatusInfoEvent': <class 'HABApp.openhab.events.thing_events.ThingFirmwareStatusInfoEvent'>, 'ThingConfigStatusInfoEvent': <class 'HABApp.openhab.events.thing_events.ThingConfigStatusInfoEvent'>, 'GroupItemStateChangedEvent': <class 'HABApp.openhab.events.item_events.GroupStateChangedEvent'>, 'FirmwareStatusInfoEvent': <class 'HABApp.openhab.events.thing_events.ThingFirmwareStatusInfoEvent'>, 'ConfigStatusInfoEvent': <class 'HABApp.openhab.events.thing_events.ThingConfigStatusInfoEvent'>}
         _in_dict = {'topic': 'openhab/items/weather_sensor_netatmo_rain_last_message/statechanged', 'payload': '{"type":"DateTime","value":"2023-06-17T15:31:04.754673068+0200","oldType":"DateTime","oldValue":"2023-06-17T15:31:04.734500815+0200"}', 'type': 'ItemStateChangedEvent'}
         _in_dict['payload'] = '{"type":"DateTime","value":"2023-06-17T15:31:04.754673068+0200","oldType":"DateTime","oldValue":"2023-06-17T15:31:04.734500815+0200"}'
         _in_dict['topic'] = 'openhab/items/weather_sensor_netatmo_rain_last_message/statechanged'
         _in_dict['type'] = 'ItemStateChangedEvent'
         event_type = 'ItemStateChangedEvent'
         payload = {'type': 'DateTime', 'value': '2023-06-17T15:31:04.754673068+0200', 'oldType': 'DateTime', 'oldValue': '2023-06-17T15:31:04.734500815+0200'}
         topic = 'openhab/items/weather_sensor_netatmo_rain_last_message/statechanged'
       ------------------------------------------------------------

    File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/events/item_events.py", line 37 in from_dict
    --------------------------------------------------------------------------------
         32 | @classmethod
         33 | def from_dict(cls, topic: str, payload: dict):
         34 |     # openhab/items/Ping/statechanged
         35 |     return cls(
         36 |         topic[14:-13],
    -->  37 |         map_openhab_values(payload['type'], payload['value']),
         38 |         map_openhab_values(payload['oldType'], payload['oldValue'])
         39 |     )
       ------------------------------------------------------------
         cls = <class 'HABApp.openhab.events.item_events.ItemStateChangedEvent'>
         payload = {'type': 'DateTime', 'value': '2023-06-17T15:31:04.754673068+0200', 'oldType': 'DateTime', 'oldValue': '2023-06-17T15:31:04.734500815+0200'}
         payload['oldType'] = 'DateTime'
         payload['oldValue'] = '2023-06-17T15:31:04.734500815+0200'
         payload['type'] = 'DateTime'
         payload['value'] = '2023-06-17T15:31:04.754673068+0200'
         topic = 'openhab/items/weather_sensor_netatmo_rain_last_message/statechanged'
         topic[14:-13] = 'weather_sensor_netatmo_rain_last_message'
       ------------------------------------------------------------

    File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/map_values.py", line 33 in map_openhab_values
    --------------------------------------------------------------------------------
          8 | def map_openhab_values(openhab_type: str, openhab_value: str):
          9 |     # because we preprocess the string value can be None.
         10 |     # Either remove the preprocessing or remove this here
          (...)
         29 |     if openhab_type == "String":
         30 |         return openhab_value
         32 |     if openhab_type == "DateTime":
    -->  33 |         dt = datetime.datetime.strptime(openhab_value, '%Y-%m-%dT%H:%M:%S.%f%z')
         34 |         # all datetimes from openHAB have a timezone set, so we can't easily compare them
         35 |         # --> TypeError: can't compare offset-naive and offset-aware datetimes
       ------------------------------------------------------------
         openhab_type = 'DateTime'
         openhab_type == 'UnDef' or openhab_value == 'NULL' = False
         openhab_value = '2023-06-17T15:31:04.754673068+0200'
         openhab_type == "DateTime" = True
         openhab_type == "Decimal" = False
         openhab_type == "HSB" = False
         openhab_type == "Number" = False
         openhab_type == "String" = False
         openhab_type == 'OnOff' = False
         openhab_type == 'OpenClosed' = False
         openhab_type == 'Percent' = False
         openhab_type == 'Point' = False
         openhab_type == 'Quantity' = False
         openhab_type == 'Raw' = False
         openhab_type == 'UnDef' = False
         openhab_type == 'UpDown' = False
         openhab_value == 'NULL' = False
       ------------------------------------------------------------

    --------------------------------------------------------------------------------
    Traceback (most recent call last):
      File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/connection_handler/sse_handler.py", line 32, in on_sse_event
        event = get_event(event_dict)
      File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/map_events.py", line 44, in get_event
        return _events[event_type].from_dict(topic, payload)
      File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/events/item_events.py", line 37, in from_dict
        map_openhab_values(payload['type'], payload['value']),
      File "/usr/local/lib/python3.10/site-packages/HABApp/openhab/map_values.py", line 33, in map_openhab_values
        dt = datetime.datetime.strptime(openhab_value, '%Y-%m-%dT%H:%M:%S.%f%z')
      File "/usr/local/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
        tt, fraction, gmtoff_fraction = _strptime(data_string, format)
      File "/usr/local/lib/python3.10/_strptime.py", line 349, in _strptime
        raise ValueError("time data %r does not match format %r" %
    ValueError: time data '2023-06-17T15:31:04.754673068+0200' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
module
    utils
severity
    Error (3) (decorated)
source
    HABApp
timestamp
    2023-06-17 15:31:05.997 +02:00
spacemanspiff2007 commented 1 year ago

Fixed in 1.1.2