custom-components / weatheralerts

A sensor that gives you weather alerts from alerts.weather.gov.
MIT License
125 stars 15 forks source link

Unable to decode JSON error #25

Closed tomhoover closed 4 years ago

tomhoover commented 4 years ago

Version of the custom_component add6019

Describe the bug Error message in home-assistant.log

Log

2019-12-27 10:30:28 ERROR (MainThread) [custom_components.weatheralerts.sensor] 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url='https://api.weather.gov/alerts/active/zone/TXZ159
2019-12-27 10:38:20 ERROR (MainThread) [custom_components.weatheralerts.sensor]

Additional info:

  1. I found it odd the first error message didn't include a trailing ' after the url (there is one at the beginning of the url).
  2. The second error message contained no descriptive text.

I don't know if either of the above items have anything to do with the error, but I wanted to provide complete information.

Finally, right after I noticed the error in the log, the link above was returning:

{
    "type": "FeatureCollection",
    "features": [],
    "title": "current watches, warnings, and advisories for McLennan (TXZ159) TX",
    "updated": "2019-12-26T17:01:08+00:00"
}
GP920 commented 4 years ago

Same. Here's my error:

Log Details (ERROR)
Fri Dec 27 2019 12:18:36 GMT-0500 (Eastern Standard Time)
0, message='Attempt to decode JSON with unexpected mimetype: text/html', url='https://api.weather.gov/alerts/active/zone/NJZ004
tomhoover commented 4 years ago

@ludeeus, I don't know Python very well, but I looked thru the code the best I could. From what I can tell, there isn't a bug in the component--it appears the weather.gov api must occasionally be returning bad data. I really don't know what more can be done (other than suppressing the error when the api returns invalid json, but that would defeat the purpose of logging). Unless you have some other ideas, I'm fine with closing this one out.

ludeeus commented 4 years ago

You are correct @tomhoover This is a result of an issue with the remote server and not the integration itself.

But... That particular error is not one of the prettiest I have seen. I just pushed an updated with a more sensible set of error handling.

Logic on update failure: 1: Log a sensible error message, with the zone id and the exception type. 2: Only log that once (pr. sensor). 3: On a successful update log an info message stating the update was successful.

This kind of logic is more like what HA requires for core integrations. https://developers.home-assistant.io/docs/en/integration_quality_scale_index.html#silver-%F0%9F%A5%88

tomhoover commented 4 years ago

Thanks for the update. Reviewing the changes will also serve me well in increasing my python knowledge! 😄