tache / homeassistant-kidde

A custom component for Kidde HomeSafe.
MIT License
14 stars 2 forks source link

ValueError: time data '2024-06-22T16:00' does not match format '%Y-%m-%dT%H:%M:%S.%f' #7

Closed agarkauskas closed 2 weeks ago

agarkauskas commented 2 weeks ago

System Health details

System Information

version core-2024.6.4
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.2
os_name Linux
os_version 6.6.33-haos
arch x86_64
timezone America/New_York
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 4976 Installed Version | 1.34.0 Stage | running Available Repositories | 1460 Downloaded Repositories | 4 HACS Data | ok
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 12.4 -- | -- update_channel | stable supervisor_version | supervisor-2024.06.0 agent_version | 1.6.0 docker_version | 26.1.4 disk_total | 30.8 GB disk_used | 8.0 GB healthy | true supported | true host_connectivity | true supervisor_connectivity | true ntp_synchronized | true virtualization | kvm board | ova supervisor_api | ok version_api | ok installed_addons | Matter Server (6.1.2), File editor (5.8.0), Terminal & SSH (9.14.0), Samba share (12.3.1)
Dashboards dashboards | 9 -- | -- resources | 0 views | 9 mode | storage
Recorder oldest_recorder_run | June 17, 2024 at 7:07 PM -- | -- current_recorder_run | June 23, 2024 at 9:23 AM estimated_db_size | 1293.62 MiB database_engine | sqlite database_version | 3.44.2

Checklist

Describe the issue

For some reason my system does not return the date in the expected format by the integration code.

Reproduction steps

  1. Downloaded the custom integration
  2. Install it
  3. It works but it create this exceptions in the log. Returned time data from my system does not contain seconds and/or milliseconds not sure why

Debug logs

Logger: homeassistant
Source: custom_components/kidde/sensor.py:163
integration: Kidde HomeSafe (documentation, issues)
First occurred: 9:42:17 AM (41 occurrences)
Last logged: 9:45:37 AM
Error doing job: Task exception was never retrieved (None)

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 255, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 411, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 491, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1000, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1126, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1061, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 543, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kidde/sensor.py", line 163, in native_value
    return datetime.datetime.strptime(value[:-4], "%Y-%m-%dT%H:%M:%S.%f").replace(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/_strptime.py", line 554, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/_strptime.py", line 333, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2024-06-22T16:00' does not match format '%Y-%m-%dT%H:%M:%S.%f'

Diagnostics dump

No response

agarkauskas commented 2 weeks ago

Some data from my smoke detectors:: Alarm Model P4010ACS_w_1381 WM Version 2.5 Alarm version 1.10 Net version 4.4.1

agarkauskas commented 2 weeks ago

I updated this line from

return datetime.datetime.strptime(value[:-4], "%Y-%m-%dT%H:%M:%S.%f")

to

return datetime.datetime.strptime(value[:-4], "%Y-%m-%dT%H:%M")

And I got a different error:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 255, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 411, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 491, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1000, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1126, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1061, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 543, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kidde/sensor.py", line 163, in native_value
    return datetime.datetime.strptime(value[:-4], "%Y-%m-%dT%H:%M").replace(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/_strptime.py", line 554, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/_strptime.py", line 336, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: :19.734902
carterbox commented 2 weeks ago

Hi @agarkauskas,

I implemented the new entity code that tries to convert the string returned from the Kidde API into a Python DateTime. Can you turn on debug logging for the integration so we can see what string is actually being returned? I have logging in there to print the string as returned from the Kidde API before it is modified to try and convert to a DateTime.

Clearly the problem is related to the seconds/micro/(nano?)seconds part of the string, but it's unclear to me from your error outputs whether your device returns variable numbers of microseconds or just a different number of microseconds from my device. Thanks!

agarkauskas commented 2 weeks ago

This is what I have here:

2024-06-23 18:53:14.420 DEBUG (MainThread) [custom_components.kidde.coordinator] Finished fetching kidde data in 0.291 seconds (success: True)
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] last_seen of type <class 'str'> is 2024-06-23T14:02:19.734902169Z
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] last_test_time of type <class 'NoneType'> is None
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] smoke_level of type <class 'int'> is 4
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] co_level of type <class 'int'> is 0
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] battery_state of type <class 'str'> is ok
2024-06-23 18:53:14.422 DEBUG (MainThread) [custom_components.kidde.sensor] life of type <class 'int'> is 535
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] last_seen of type <class 'str'> is 2024-06-23T22:09:43.999904681Z
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] last_test_time of type <class 'NoneType'> is None
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] smoke_level of type <class 'int'> is 3
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] co_level of type <class 'int'> is 0
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] battery_state of type <class 'str'> is ok
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] life of type <class 'int'> is 535
2024-06-23 18:53:14.423 DEBUG (MainThread) [custom_components.kidde.sensor] last_seen of type <class 'str'> is 2024-06-23T22:36:49.985605162Z
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] last_test_time of type <class 'NoneType'> is None
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] smoke_level of type <class 'int'> is 0
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] co_level of type <class 'int'> is 0
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] battery_state of type <class 'str'> is ok
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] life of type <class 'int'> is 535
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] last_seen of type <class 'str'> is 2024-06-23T12:43:32.26698786Z
2024-06-23 18:53:14.424 DEBUG (MainThread) [custom_components.kidde.sensor] last_test_time of type <class 'str'> is 2024-06-22T16:00:19Z

Hope it helps. Two different date formats. Now where the Z came from please dont ask me.

carterbox commented 2 weeks ago

Thanks! Looks like the last_test_time is reported at a different precision than last_seen. I haven't actually tested my devices yet, so that's probably why I haven't run into this issue yet. 😄

Now where the Z came from please dont ask me.

My understanding is that Z is the timezone code. Z means the time is reported as UTC instead of local time.