Reviewers needed: Environment Canada component with weather, sensor, and camera (radar) platforms

Thanks, but unfortunately the PR hasn’t made any progress. I suggest that you do a custom install for now, as things have been fairly stable for a few weeks. Let me know if you have any trouble.

1 Like

Here’s a quick Python script I threw together to retrieve the details of weather alerts/statements/watches from Environment Canada.

The script outputs json that can be used in conjunction with a command_line sensor. You can then display the alert details in the frontend. You’ll need to pick your location from this page, and update the URL variable in the script. My sensor is configured as follows:

sensor:
 - platform: command_line
   command: "/path/to/env_canada_alerts.py"
   json_attributes:
    - date
    - title
    - details
   value_template: '{{ value_json.title }}'
   scan_interval: 3600

And here is what is looks like:
52
Suggestions or comments welcome

1 Like

Nice! I’ll try to work that into the sensor. I’d love to be able to set the location automatically as well, but the warning areas are weird. If the publish the GIS data, maybe as SHP files, I might be able to work it out…

That would be great! I agree that it would be better to get the location pulled automatically. By the way I went the scraping/BeautifulSoup route because I couldn’t find a plain XML API to get the actual text descriptions of the alerts.

Yes, that’s a disappointing aspect of EnviroCan’s ATOM feed. It’s missing the actual details of the alert.

FWIW, it’s been that way since I can remember now (at least 8 years) and, for the EnviroCan driver I created, I was obliged to use web-scraping as well to extract the alert’s details. Over the last 8 years I think I had to change my web-scraping algorithm twice because EnviroCan changed the names of the CSS tags.

PS
I’m also waiting for the big deluge to hit later today. One of those times I’m glad I don’t own a home in a low-lying, or waterfront, area.

Okay, based on @davidbb’s work I’ve added alert details to the sensor. Here’s how it looks:

To update:

  • Install the updated env_canada library:
    • For Docker: docker exec [container name] pip install env_canada==0.0.9 --target /config/deps/lib/python3.6/site-packages --upgrade
    • Otherwise: pip install env_canada==0.0.9 --target [config folder]/deps/lib/python3.6/site-packages --upgrade
  • Place the files from Dropbox in a folder at [config folder]/custom_components/environment_canada/
  • Restart your Home Assistant installation

Let me know what you think, or if you have any trouble.

This looks great! Just wanted to confirm: this feature is available in your Dropbox but hasn’t been put into your fork of HA right? I’m looking here.

Yeah, they’re starting to diverge a bit because of changes to how the requirements are being loaded, I think starting in 0.92. I’ll add it into the fork as well shortly.

@michaeldavie I pulled down the latest version of all the files in your github repo and upgraded to 0.92.1. Also installed env_canada==0.0.9 Everything is working well except sensor.warnings. There is currently a rainfall warning in my area and I’m getting this error on start:

Traceback (most recent call last):
File “/usr/local/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py”, line 261, in _async_add_entity
await entity.async_device_update(warning=False)
File “/usr/local/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 377, in async_device_update
await self.hass.async_add_executor_job(self.update)
File “/usr/local/lib/python3.5/asyncio/futures.py”, line 381, in iter
yield self # This tells Task to wait for completion.
File “/usr/local/lib/python3.5/asyncio/tasks.py”, line 310, in _wakeup
future.result()
File “/usr/local/lib/python3.5/asyncio/futures.py”, line 294, in result
raise self._exception
File “/usr/local/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/usr/local/lib/python3.5/site-packages/homeassistant/util/init.py”, line 224, in wrapper
result = method(*args, **kwargs)
File “/root/.homeassistant/custom_components/environment_canada/sensor.py”, line 153, in update
for s in sensor_data])
File “/root/.homeassistant/custom_components/environment_canada/sensor.py”, line 153, in
for s in sensor_data])
AttributeError: ‘str’ object has no attribute ‘get’

Any thoughts on what the problem could be?

edit: In case you see this and the warning is no longer posted, I saved the xml file and the warnings page HTML.

It seems that your installation is still trying to use v0.0.8. In that version each alert was a string, while in v0.0.9 each one is a dictionary. Have you tried restarting your server?

Ah right the restart thing. I restarted once more and the alert details are now showing up and the errors are gone. I still don’t know why upgrading env_canada often needs several reboots, but in any case it’s working now. Thanks!

Everything working here with v0.0.9 :grinning:

Now getting this error:

Error while setting up platform environment_canada
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py”, line 126, in _async_setup_platform
SLOW_SETUP_MAX_WAIT, loop=hass.loop)
File “/usr/local/lib/python3.7/asyncio/tasks.py”, line 416, in wait_for
return fut.result()
File “/usr/local/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/environment_canada/weather.py”, line 71, in setup_platform
ec_data = ECData(station_id=config[CONF_STATION])
File “/config/deps/lib/python3.7/site-packages/env_canada/env_canada.py”, line 67, in init
self.update()
File “/config/deps/lib/python3.7/site-packages/env_canada/env_canada.py”, line 109, in update
‘detail’: alert_soup.select(detail_pattern.format(heading))[0].text}
IndexError: list index out of range

Thanks, I’ve noticed some weirdness like this too. I think the cause may be the alert still showing up in the XML, but not being on the alert webpage anymore. In any case, I think I know how to fix it.

It eventually went away on its own. That seems to be consistent with your hunch as to it’s cause.

Okay, I made an adjustment in v0.0.10. Please give that a try. On my system I just had to edit the manifest.json file to require the new version and restart.

Update: Nope, had a similar problem today. I’ll keep tweaking.

I would love to try this component but i am completely confused on how to install it. I am running a plain setup on a CentOS VM and have attempted to follow the above instructions to no avail.

I would be gratefull for a bit of help. I am running Home Assistant 0.92.2 and my config is locate here:
/home/homeassistant/std/homeassistant

I ran the following commands:

sudo -u homeassistant -H -s
pip3 install env_canada==0.0.9 --target /home/homeassistant/std/homeassistant/deps/lib/python3.6/site-packages

With the following result:

Successfully installed beautifulsoup4-4.7.1 certifi-2019.3.9 chardet-3.0.4 env-canada-0.0.9 geographiclib-1.49 geopy-1.19.0 idna-2.8 imageio-2.5.0 numpy-1.16.3 pillow-6.0.0 requests-2.21.0 requests-futures-0.9.9 soupsieve-1.9.1 urllib3-1.24.3

Then I downloaded the following four files from the Dropbox:

__ init__.py, camera.py, sensor.py, weather.py

and installed them here:

/home/homeassistant/std/homeassistant/custom_components/environment_canada/…

I am not sure what is my next step? I restarted the service but I do not see any sensors or any mention in the log that the component is being loaded.

Sorry, I haven’t been keeping the Dropbox files up to date, but I’ve updated them just now.

Please copy over the latest files from Dropbox, including the manifest.json, and restart. I’m not sure that you need to install the env_canada package manually anymore, but if you get an error related to that please do the same install command as above but with env_canada==0.0.10.

Hopefully that will work for you, but if not please let me know.

Please Michael do not apologies, your efforts are much appreciated. I did as you suggested but I must be doing something fundamentally wrong in my setup. I see no mention of your component in the logs.

I can see in the thread that for the docker installation it was mentioned that ec_sensor.py was pointing to environment_canada.py

-v ~/home-assistant:/config
-v ~/home-assistant/ec_sensor.py:/usr/src/app/homeassistant/components/sensor/environment_canada.py \

Is this still relevant? I do not even have environment_canada.py file on my server but I do have env_canada.py installed:

/home/homeassistant/std/homeassistant/deps/lib/python3.6/site-packages/env_canada/env_canada.py

Have you added the components to your configuration.yaml? You need:

weather:
  - platform: environment_canada
sensor:
  - platform: environment_canada
camera:
  - platform: environment_canada