barleybobs / homeassistant-ecowater-softener

A Homeassistant custom component to integrate Ecowater water softeners
https://github.com/barleybobs/homeassistant-ecowater-softener
MIT License
33 stars 10 forks source link

Error: time data 'Today' does not match format '%d/%m/%Y' #6

Closed mbauwens closed 2 years ago

mbauwens commented 2 years ago

When the EcoWater softener is out of salt, it throws an error because the output is just "Today" and not a date format:

This error originated from a custom integration.

Logger: custom_components.ecowater_softener.sensor
Source: custom_components/ecowater_softener/sensor.py:120
Integration: Ecowater Softener (documentation, issues)
First occurred: 03:48:42 (1 occurrences)
Last logged: 03:48:42

Error: time data 'Today' does not match format '%d/%m/%Y'
Traceback (most recent call last):
  File "/config/custom_components/ecowater_softener/sensor.py", line 120, in async_update
    self._attrs[ATTR_OUT_OF_SALT_ON] = datetime.strptime(data_json['out_of_salt'], '%d/%m/%Y').strftime('%Y-%m-%d')
  File "/usr/local/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/local/lib/python3.9/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data 'Today' does not match format '%d/%m/%Y'
mbauwens commented 2 years ago

Maybe not the most robust solution, but I did manage to fix it manually in my own files by editing sensor.py:

Suggestion via pr: #7

if data_json['out_of_salt'] == "Today":
    self._attrs[ATTR_OUT_OF_SALT_ON] = "Today"
else:
    if self._dateformat == "dd/mm/yyyy":
        self._attrs[ATTR_OUT_OF_SALT_ON] = datetime.strptime(data_json['out_of_salt'], '%d/%m/%Y').strftime('%Y-%m-%d')
    elif self._dateformat == "mm/dd/yyyy":
        self._attrs[ATTR_OUT_OF_SALT_ON] = datetime.strptime(data_json['out_of_salt'], '%m/%d/%Y').strftime('%Y-%m-%d')
    else:
        self._attrs[ATTR_OUT_OF_SALT_ON] = ''
        _LOGGER.exception(
            f"Error: Date format not set"
        )
barleybobs commented 2 years ago

Fixing it now! Sorry I haven't been very active lately.

Thanks 👍