davidrapan / ha-solarman

⚡ Solarman Stick Logger integration for 🏠 Home Assistant
MIT License
73 stars 18 forks source link

Datetime Problem #194

Open CrazyUs3r opened 1 day ago

CrazyUs3r commented 1 day ago

I downloaded everything new this morning and noticed that datetime was showing an error

TypeError: strptime() argument 1 must be str, not datetime.datetime

here is my suggestion

@property
def native_value(self) -> datetime | None:
    """Return the value reported by the datetime."""
    if not self._attr_native_value:
        return None

    if isinstance(self._attr_native_value, datetime):
        if self._attr_native_value.tzinfo is None:
            return self._attr_native_value.replace(tzinfo=ZoneInfo(self.coordinator.hass.config.time_zone))
        return self._attr_native_value

    return datetime.strptime(self._attr_native_value, '%y/%m/%d %H:%M:%S').replace(tzinfo=ZoneInfo(self.coordinator.hass.config.time_zone))
davidrapan commented 1 day ago

refactor: Moved Date/Time formats into constants should also fix it I guess?

CrazyUs3r commented 1 day ago

I try it later

CrazyUs3r commented 1 day ago

it works but, if I enter e.g. 4 pm it will be written 2 pm

davidrapan commented 1 day ago

I'm not able to reproduce it.

davidrapan commented 1 day ago

Replace method in datetime.py at line ~64 with:

    async def async_set_value(self, value: datetime) -> None:
        """Change the date/time."""
        _LOGGER.warning(value)
        _LOGGER.warning(value.astimezone(ZoneInfo(self.coordinator.hass.config.time_zone)))
        _LOGGER.warning(get_dt_as_list_int(value, self._multiple_registers))
        _LOGGER.warning(get_dt_as_list_int(value.astimezone(ZoneInfo(self.coordinator.hass.config.time_zone)), self._multiple_registers))
        #if await self.coordinator.inverter.call(CODE.WRITE_MULTIPLE_HOLDING_REGISTERS, self.register, get_dt_as_list_int(value, self._multiple_registers), ACTION_ATTEMPTS_MAX) > 0:
        #    self.set_state(value.strftime(DATETIME_FORMAT))
        #    self.async_write_ha_state()

Restart HA, try to set datetime and show me what's in the log. Also don't forget to mention which datetime did you set.

CrazyUs3r commented 1 day ago

at germany is 17:59 an i write to 16

2024-10-06 17:58:32.110 WARNING (MainThread) [custom_components.solarman.datetime] 2024-10-06 14:58:05+00:00 2024-10-06 17:58:32.110 WARNING (MainThread) [custom_components.solarman.datetime] 2024-10-06 16:58:05+02:00 2024-10-06 17:58:32.110 WARNING (MainThread) [custom_components.solarman.datetime] [6154, 1550, 14853] 2024-10-06 17:58:32.110 WARNING (MainThread) [custom_components.solarman.datetime] [6154, 1552, 14853]

davidrapan commented 1 day ago

Do you have correct time zone setup in your pc/phone?

CrazyUs3r commented 1 day ago

at my windows laptop is it correct and my phone the same, in ha log is the correct time from ha

davidrapan commented 1 day ago

In my case values between rows 1 and 2 are not different... so I don't understand why they are in yours...

davidrapan commented 1 day ago

How is your HA installed?

CrazyUs3r commented 1 day ago

my ha core is installed under proxmox on a mini server every log from ha has the correct locale time

davidrapan commented 1 day ago

What if you try to set datetime using Developer tools > ACTIONS > Date/Time: Set date/time?

CrazyUs3r commented 1 day ago

I use the config from the yaml grafik

davidrapan commented 1 day ago

Sure but try it using ACTIONS too and tell me result.

CrazyUs3r commented 1 day ago

There is nothing in the log that I can send you, and the time that is read does not change

davidrapan commented 1 day ago

What? You did not do it properly then. Nvm I found the issue, it looks like it's due to a bug in the HA.

davidrapan commented 1 day ago

fix: Inconsistent behavior of datetime set_value

CrazyUs3r commented 1 day ago

I'll test it tomorrow, there's no sun today but then I'll see tomorrow morning what other errors there are. I've already loaded the datetime

CrazyUs3r commented 1 day ago

But now i See

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

''' Logger: homeassistant Quelle: custom_components/solarman/datetime.py:62 Integration: Solarman (Dokumentation, Probleme) Erstmals aufgetreten: 18:42:19 (3 Vorkommnisse) Zuletzt protokolliert: 18:45:23

Error doing job: Task exception was never retrieved (None) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval await self._async_refresh(log_failures=True, scheduled=True) File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 453, in _async_refresh self.async_update_listeners() File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners update_callback() File "/config/custom_components/solarman/entity.py", line 44, in _handle_coordinator_update self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state self.async_calculate_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/datetime/init.py", line 104, in state if (value := self.native_value) is None: ^^^^^^^^^^^^^^^^^ File "/config/custom_components/solarman/datetime.py", line 62, in native_value return datetime.strptime(self._attr_native_value, DATETIME_FORMAT).replace(tzinfo = ZoneInfo(self.coordinator.hass.config.time_zone)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 '48/0/0 0:0:0' does not match format '%y/%m/%d %H:%M:%S' '''

And

''' Logger: homeassistant Quelle: components/sensor/init.py:636 Erstmals aufgetreten: 18:42:13 (3 Vorkommnisse) Zuletzt protokolliert: 18:44:19

Error doing job: Task exception was never retrieved (None) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval await self._async_refresh(log_failures=True, scheduled=True) File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 453, in _async_refresh self.async_update_listeners() File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners update_callback() File "/config/custom_components/solarman/entity.py", line 44, in _handle_coordinator_update self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state self.async_calculate_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/sensor/init.py", line 636, in state raise ValueError( ValueError: Sensor sensor.solarmanm80_device provides state value '', which is not in the list of options provided '''

davidrapan commented 1 day ago

I guess this was during inverter shutdown?

CrazyUs3r commented 1 day ago

I assume so too, I just wanted to show you. It hasn't lost WiFi in the meantime and there was no other log entry either.

davidrapan commented 1 day ago

Sure! I'll look where would be best to put some additional checks so it doesn't happen even during that. Thanks! 😉

CrazyUs3r commented 13 hours ago

So to day errors

Logger: homeassistant Quelle: components/sensor/init.py:636 Erstmals aufgetreten: 07:44:55 (20 Vorkommnisse) Zuletzt protokolliert: 16:35:26

Error doing job: Task exception was never retrieved (None) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval await self._async_refresh(log_failures=True, scheduled=True) File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 453, in _async_refresh self.async_update_listeners() File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners update_callback() File "/config/custom_components/solarman/entity.py", line 44, in _handle_coordinator_update self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state self.async_calculate_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/sensor/init.py", line 636, in state raise ValueError( ValueError: Sensor sensor.solarmanm80_device provides state value '', which is not in the list of options provided

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: homeassistant Quelle: custom_components/solarman/datetime.py:62 Integration: Solarman (Dokumentation, Probleme) Erstmals aufgetreten: 07:43:24 (2 Vorkommnisse) Zuletzt protokolliert: 07:45:00

Error doing job: Task exception was never retrieved (None) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval await self._async_refresh(log_failures=True, scheduled=True) File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 453, in _async_refresh self.async_update_listeners() File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners update_callback() File "/config/custom_components/solarman/entity.py", line 44, in _handle_coordinator_update self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state self.async_calculate_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/datetime/init.py", line 104, in state if (value := self.native_value) is None: ^^^^^^^^^^^^^^^^^ File "/config/custom_components/solarman/datetime.py", line 62, in native_value return datetime.strptime(self._attr_native_value, DATETIME_FORMAT).replace(tzinfo = ZoneInfo(self.coordinator.hass.config.time_zone)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 '48/0/0 0:0:0' does not match format '%y/%m/%d %H:%M:%S'

Logger: homeassistant.components.datetime Quelle: helpers/entity_platform.py:595 Integration: Datum/Uhrzeit (Dokumentation, Probleme) Erstmals aufgetreten: 07:43:19 (1 Vorkommnisse) Zuletzt protokolliert: 07:43:19

Error adding entity datetime.solarmanm80_date_time for domain datetime with platform solarman Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 595, in _async_add_entities await coro File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 909, in _async_add_entity await entity.add_to_platform_finish() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1366, in add_to_platform_finish self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state self.async_calculate_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/datetime/init.py", line 104, in state if (value := self.native_value) is None: ^^^^^^^^^^^^^^^^^ File "/config/custom_components/solarman/datetime.py", line 62, in native_value return datetime.strptime(self._attr_native_value, DATETIME_FORMAT).replace(tzinfo = ZoneInfo(self.coordinator.hass.config.time_zone)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 '48/0/0 0:0:0' does not match format '%y/%m/%d %H:%M:%S'

davidrapan commented 13 hours ago

fix: ValueError: time data does not match format