home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.83k stars 28.94k forks source link

Environment Canada Weather fails to start #50737

Closed lukewaite closed 2 years ago

lukewaite commented 3 years ago

The problem

Upon initial installation of the Environment Canada weather integration, the integration fails to start.

What is version of Home Assistant Core has the issue?

2021.5.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Environment Canada

Link to integration documentation on our website

https://www.home-assistant.io/integrations/environment_canada/#weather

Example YAML snippet

weather:
  - platform: environment_canada

### Anything in the logs that might be useful for us?

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 364, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 558, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 581, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 334, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 368, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 340, in _stringify_state
    state = self.state
  File "/usr/src/homeassistant/homeassistant/components/weather/__init__.py", line 206, in state
    return self.condition
  File "/usr/src/homeassistant/homeassistant/components/environment_canada/weather.py", line 164, in condition
    elif self.ec_data.hourly_forecasts[0].get("icon_code"):
IndexError: list index out of range

Additional information

No response

probot-home-assistant[bot] commented 3 years ago

environment_canada documentation environment_canada source (message by IssueLinks)

lukewaite commented 3 years ago

Looks like my local station appears to have no current state: image

Vs the Ottawa station: image

clarjon1 commented 3 years ago

I've been running into this issue, with the exact same stacktrace, so I won't duplicate that.

When the state becomes available on the environment canada site, I've found I need to restart hass to get the integration to show up. Perhaps it needs to be changed to accept no current state on startup, instead of erroring out fully for that boot?

github-actions[bot] commented 2 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

campbellr commented 2 years ago

~This has been happening to ever since upgrading from 2021.7.3 to 2021.8.6 the other day, so it is still an issue.~

~I haven't had a chance to dig into it yet but I'll try to do some debugging later. FWIW, I'm using this station: https://dd.weather.gc.ca/citypage_weather/xml/AB/s0000510_e.xml~

EDIT: I just saw there was a new point release, and after upgrading, it seems to be working again. I'm not sure if that's because there was an actual fix, or if restarting HA this time finally resolved it.

campbellr commented 2 years ago

It turns out that my restart was just a fluke. I am still hitting this quite regularly.

I did a bit of digging and I think this might be caused by some rate limiting that is baked into the env_canada library:

https://github.com/michaeldavie/env_canada/blob/v0.2.5/env_canada/ec_data.py#L244-L246

Note that there is a pretty low rate limit of 2 calls per minute, at which point the update() method will be skipped. This isn't much of an issue after the ECData object is initialized, but if we hit the rate limit error inside __init__(), then none of the attributes will be populated and we would see an IndexError like in the tracebacks above.

eg:

>>> ec_data = ECData(coordinates=coords); print(len(ec_data.hourly_forecasts))
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /citypage_weather/docs/site_list_en.csv HTTP/1.1" 200 32894
DEBUG:env_canada.ec_data:station_id = AB/s0000510
DEBUG:env_canada.ec_data:in update
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /citypage_weather/xml/AB/s0000510_e.xml HTTP/1.1" 200 2732
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/doc/AQHI_XML_File_List.xml HTTP/1.1" 200 6313
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/aqhi/pnr/observation/realtime/xml/AQ_OBS_IACMP_CURRENT.xml HTTP/1.1" 200 525
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/aqhi/pnr/forecast/realtime/xml/AQ_FCST_IACMP_CURRENT.xml HTTP/1.1" 200 734
24
>>> ec_data = ECData(coordinates=coords); print(len(ec_data.hourly_forecasts))
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /citypage_weather/docs/site_list_en.csv HTTP/1.1" 200 32894
DEBUG:env_canada.ec_data:station_id = AB/s0000510
DEBUG:env_canada.ec_data:in update
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /citypage_weather/xml/AB/s0000510_e.xml HTTP/1.1" 200 2734
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/doc/AQHI_XML_File_List.xml HTTP/1.1" 200 6313
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/aqhi/pnr/observation/realtime/xml/AQ_OBS_IACMP_CURRENT.xml HTTP/1.1" 200 525
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /air_quality/aqhi/pnr/forecast/realtime/xml/AQ_FCST_IACMP_CURRENT.xml HTTP/1.1" 200 734
24
>>> ec_data = ECData(coordinates=coords); print(len(ec_data.hourly_forecasts))
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): dd.weather.gc.ca:443
DEBUG:urllib3.connectionpool:https://dd.weather.gc.ca:443 "GET /citypage_weather/docs/site_list_en.csv HTTP/1.1" 200 32894
DEBUG:env_canada.ec_data:station_id = AB/s0000510
DEBUG:env_canada.ec_data:hit rate limit: too many calls
0
michaeldavie commented 2 years ago

Please try the custom component version of this integration, which is hosted at https://github.com/michaeldavie/hass_environment_canada. This version includes a number of fixes, but is not currently part of the main HA project as it has not been integrated into the GUI config.

If this problem persists please let me know.

jjross commented 2 years ago

I can confirm that the custom component addressed the issue with the weather component but I am now getting errors when setting up the sensor component:

Sensor component:

Logger: homeassistant.components.sensor
Source: custom_components/environment_canada/sensor.py:89
Integration: Sensor (documentation, issues)
First occurred: 10:10:52 PM (1 occurrences)
Last logged: 10:10:52 PM

Error while setting up environment_canada platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/environment_canada/sensor.py", line 89, in async_setup_platform
    async_add_entities(
TypeError: 'NoneType' object is not iterable
michaeldavie commented 2 years ago

That's odd. Can you please provide some more info on your installation and configuration?

campbellr commented 2 years ago

@michaeldavie the custom integration seems to be working for me, thanks!

The only issue I've noticed so far is that it seems to be missing the air_quality_health_index sensor that the builtin integration exposes.

michaeldavie commented 2 years ago

@gwww Are you up for adding another sensor type? I moved the AQHI data into a separate ECAirQuality class in the library since it's a completely different data source. However, it's unique data provided by Environment Canada so it would probably be worth including.

gwww commented 2 years ago

Will look at it.

michaeldavie commented 2 years ago

@campbellr AQHI has been added back in: image

jm66 commented 2 years ago

hey @michaeldavie, was the AQHI removed?

gwww commented 2 years ago

It’s coming back. PRs are backlogged. We’re looking at seeing if that work can be accelerated.

github-actions[bot] commented 2 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.