Eugeniusz-Gienek / kodi_weather_ha

GNU General Public License v3.0
7 stars 0 forks source link

Error when the HA sensor has missing values #4

Open mvn23 opened 1 month ago

mvn23 commented 1 month ago

When the response from HA is missing some forecasted values, the script will throw an error in the Kodi log. In my case the weather provider does not have humidity in the forecast:

2024-07-28 18:49:54.978 T:1592    error <general>: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                                    - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                                   Error Type: <class 'ValueError'>
                                                   Error Contents: invalid literal for int() with base 10: ''
                                                   Traceback (most recent call last):
                                                     File "/home/fry//.kodi/addons/weather.ha/default.py", line 8, in <module>
                                                       ha_weather.MAIN(mode=sys.argv[1],w=WINDOW)
                                                     File "/home/fry//.kodi/addons/weather.ha/lib/ha_weather.py", line 85, in __init__
                                                       self.getForecasts()
                                                     File "/home/fry//.kodi/addons/weather.ha/lib/ha_weather.py", line 248, in getForecasts
                                                       set_property('Daily.'+str(i+1)+'.DewPoint'          , convert_temp(dewpoint(int(convert_temp(forecast['day'+count+'_temperature'], temperature_unit,'C')), int(forecast['day'+count+'_humidity'])), 'C',None), WND)
                                                                                                                                                                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                                   ValueError: invalid literal for int() with base 10: ''
                                                   -->End of Python script error report<--

One solution would be to provide bogus values in the HA template sensor, but it would be cleaner if the script handled it correctly and skipped the missing values.

For now I removed the humidity and the dew point from the forecast in ha_weather.py here https://github.com/Eugeniusz-Gienek/kodi_weather_ha/blob/8ae89ce934d670b69681289b6caf70f94d37b5e5/lib/ha_weather.py#L243 and here https://github.com/Eugeniusz-Gienek/kodi_weather_ha/blob/8ae89ce934d670b69681289b6caf70f94d37b5e5/lib/ha_weather.py#L248

Eugeniusz-Gienek commented 3 weeks ago

Sure, good idea. I'll take a look at how to make it the best way