Support for Environment Canada platforms

I found a tool to download the folder as a ZIP file. If you copy the environment_canada folder from the ZIP file into the custom_components folder, you should be all set.

GitZip: GitZip
Github Folder: home-assistant/homeassistant/components/environment_canada at env_canada_0-3-0 · michaeldavie/home-assistant · GitHub

Let me know if you have any trouble.

Awesome! Thank you! I’ll give it a shot.

Logger: homeassistant.components.weather
Source: components/environment_canada/weather.py:164
Integration: Weather (documentation, issues)
First occurred: 8:27:07 AM (2 occurrences)
Last logged: 8:27:07 AM

Error adding entities for domain weather with platform environment_canada
Error while setting up environment_canada platform for weather
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 383, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 588, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 615, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 368, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 402, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 374, 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

It seems like the hourly_forecasts list was either missing or empty because attempting to get its zeroth item resulted in failure. Perhaps the weather location you are using didn’t have hourly forecast data at the time it was polled.

I’ve only ever used daily

weather:
  - platform: environment_canada
    name: richmond
    station: BC/s0000862
    forecast: daily

I’ve tried removing station:, forecast:, name: and after 8 or so reboots, it loaded weather.richmond once but that was just a fluke. Two restarts after that with no changes and it resumed failing again. It’s been failing for a couple of months now…

@dbrunt This configuration works fine for me in 2021.7.3:

Can you provide some more info on your setup? I suspect that the dependencies may have gotten out of sync and a reinstall / upgrade may resolve it.

I wasn’t sure how to reinstall / upgrade so I just commented out the weather: items in configuration & sensors.yaml and restarted HA. Then I re-enabled them, restarted HA and I am back in business…

weather:
  - platform: environment_canada
    name: Vancouver
    station: BC/s0000141
#    name: Richmond
#    station: BC/s0000862
    forecast: daily
    scan_interval: 600
sensors:
# Environment Canada
  - platform: environment_canada
#    Richmond
#    station: BC/s0000862
#    Vancouver
    station: BC/s0000141

Thanks!

Hello, Do you know how to format the text contained in the forecast sensor? I tried a Markdown, but it doesn’t work.

How do you want to format it?

First, I’d like to align the text to the left. And also better presented.

I don’t know why it appears right-justified in your example. I created the following simple Markdown card and the content is rendered left-justified and simply wraps to the next line.

Does your Markdown card’s Content field contain the same template or something different?

Thanks to you, It works now. It’s a bit ridiculous of me, I forgot to put an s in the word states Hen i use a Markdown card.

sensor.temperature doesn’t update consistently. Sometimes it stays stuck for hours, restarting HA makes it update for a while and then it happens again

weather:
  - platform: environment_canada
sensor:
  - platform: environment_canada
    scan_interval: 600
1 Like

Like previous post, forecast not updated for hours. Found this in my log:

2021-08-27 03:00:07 ERROR (MainThread) [homeassistant.helpers.entity] Update for weather.ottawa fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 446, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 654, in async_device_update
    raise exc
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/environment_canada/weather.py", line 178, in update
    self.ec_data.update()
  File "/usr/local/lib/python3.9/site-packages/env_canada/ec_data.py", line 207, in res
    return fun(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/ratelimit/decorators.py", line 80, in wrapper
    return func(*args, **kargs)
  File "/usr/local/lib/python3.9/site-packages/env_canada/ec_data.py", line 257, in update
    weather_tree = et.fromstring(weather_xml)
  File "/usr/local/lib/python3.9/xml/etree/ElementTree.py", line 1348, in XML
    return parser.close()
xml.etree.ElementTree.ParseError: unclosed token: line 801, column 1

What I really don’t understand is that the version of the lib being used is 0.2.5 and the latest on GitHub is 0.5.0. I’m running the very latest HA.

What else I don’t understand is that I cannot find the 0.2.5 version on GitHub (looking back through history). I see 0.2.4 followed by a bump to 0.3.0. I can find the 0.2.5 version on PyPi which I downloaded to look at the code.

Looking at the latest 0.5.0 code I still see the XML decode logic is the same as 0.2.5 version (env_canada/ec_weather.py at 4ce3ac967272ce7e76c336768bf032d85fb6da52 · michaeldavie/env_canada · GitHub). At a minimum should that code be in a try block to protect against bad data? That is the exception I’m seeing and have posted here. Beyond a try block there probably should be some kind of retry logic, if that is not already there.

I also notice that the 0.5.0 version is using asyncio. Yeah!!! Given that changes the HA integration, I believe, I think I understand why HA is not using this code yet. If it is going to be a while until the asyncio version is ready, does it make sense to put out a 0.2.6 with the try block fix?

What are there plans to update to the latest lib? Is help required… testing, coding, other?

Here’s a summary of the current situation:

I would certainly appreciate any help that anyone is able to contribute, particularly on integration with the config UI. Once that’s out of the way, merging the updates from the custom component back into the main HA project should be fairly straightforward.

1 Like

Can you shed more light on the custom component ?. Should I disable the one in HA and copy your repo into custom component folder and manually modify configuration.yaml ?
Are there any new features in the custom component or only fixes ?

Thanks for the update. I understand where all the moving parts are now. I’m willing to pluck away as the integration part based on the new library (0.5.0). No guarantees and it will likely be 1-2 months to get it done (just because I’m busy with other stuff).

First comment on the 0.5.0 lib. I tried it in a virtual environment and it failed to run the example code. lxml was missing. Once installed I could run. However, in poking around to figure out what lxml was I found that everyone integrating with HA is using defusedxml. Since this would make integration with HA more consistent would it make sense to make the switch?

Next thing I’ve found as I poke away… Looking at the source of the weather data there is a newer faster better (all singing and dancing :wink: ) weather server. On this page it refers to this new server with 10x bandwidth, but also with different directory structure. Swapping to that, even for the 0.2.5 version might provide some stability. I’m assuming that you can still provide bumps of the lib versions in HA.

That’s all for now! I am looking at the integration. Early thoughts are to simplify the configuration. Combining weather and sensor sections for sure. Removing hourly and daily switch as both are always retrieved and it’s the front end (I believe) that is configured as to what to display.

Some early thoughts! Next step will be to get my HA dev environment setup again.

1 Like

Yes, copy the files from the link above into custom_components/environment_canada and it will override the baseline version. The configuration should still be fine.

There are a few changes:

  • Updates seem more reliable
  • Sensors have pretty icons
  • Radar imagery is now smaller, has consistent colours, and uses a semi-transparent overlay

@gwww:

  • That would be great, I’d appreciate any help you can provide.
  • Good point on defusedxml versus lxml. It’s safer, and should be an easy switch.
  • Note that in version 0.4.0 of the library I switched to returning native Python objects where it makes sense, and the component hasn’t been updated to reflect this yet.
  • Good point on the high-speed server as well. It looks like the folders just incorporate the UTC date, so that should be doable.

Thanks again for offering to help!

I will work off the latest version of the library. Most of the changes we’re talking about are encapsulated in the lib (defusedxml and the high-speed server) so they won’t affect the integration. I’ll look at your integration as the base.

1 Like

I’ve published new releases for both branches that use the faster server, and I fixed the problem of the missing lxml (it wasn’t added to the library’s setup.py in v0.5.0).

I also realized that I’m not actually using lxml for HA, it’s only being used for the historical data.