BCHydro component - where did it go?

Can someone confirm what version it DOES work on for me? That will help!

Hmm… What versions are you referring to Courtenay?

I’m missing this sensor :cry:

Home Assistant versions.

Here’s the error: bchydro:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/xml/etree/ElementTree.py", line 1630, in feed
    self.parser.Parse(data, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 80, column 15

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 292, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 448, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  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/bchydro/sensor.py", line 62, in update
    self._state = self._api.latest_usage()
  File "/config/custom_components/bchydro/sensor.py", line 125, in latest_usage
    root = ET.fromstring(r.text)
  File "/usr/local/lib/python3.7/xml/etree/ElementTree.py", line 1315, in XML
    parser.feed(text)
  File "/usr/local/lib/python3.7/xml/etree/ElementTree.py", line 1632, in feed
    self._raiseerror(v)
  File "/usr/local/lib/python3.7/xml/etree/ElementTree.py", line 1531, in _raiseerror
    raise err
  File "<string>", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 80, column 15

I think bchydro added this bit… and maybe thats the problem:

$(document).bind("ajaxSend", function(elm, xhr, s){
         var bchydroparamVal = $('#bchydroparam').html();
         if (s.type == "POST") {  //This can be extended for GET requests also
           //for new apps
           xhr.setRequestHeader('bchydroparam', bchydroparamVal);
           //for legacy code  
           xhr.setRequestHeader('X-CSRF-Token', bchydroparamVal); 
         }
     });

Worked on this for a few hours yesterday, and at least got the login piece working in a standalone script, but it breaks when I put it back into HA. :frowning:

Hi Courtenay,

Could I please have the script you made? I think I could run it outside of hassio and then put the data back into hassio.

Thanks :slight_smile:

1 Like


It’s a real mess, and has some weird duplication cause I have no idea what im doing but … go for it!

Has anyone made any progress on this? Or a different work around? Its too bad it stopped working before I even had a chance to test it out.

Couldn’t get Courtenay’s script to run. Really hoping the component will be fixed…

Hi everyone, I think I can help - pretty new to HA but I have some experience with Python.

@Courtenay_Watson nice work, I’ve confirmed that running your standalone script does return the most recent usage number! There’s some class cleanup to be done but you already fixed the XML parsing.

I wanted to share my progress but it’s busted right now, I’m stuck on an HA module loading error. Seems common, I’ll read some more docs. Good news though: I think we can get the slid from a cookie instead of asking users to extract it from browser devtools. Looks like we could implement “config flow” and let users enter secrets in browser as well.

The data coming out of BCHydro is interesting, and I think it might affect how we collect usage. The current script checks for != 'INVALID' points, but in addition to “ACTUAL” there is a third type, “ESTIMATED”.

It looks like BCH estimates 1-2 days worth of data, but only if you happen to check the site at a particular time… I checked again later and the ESTIMATED entities were gone. If these estimations are present we might end up recording a number that isn’t accurate. To counter this we could only extract the latest ACTUAL point, but that might be 1-2 days in the past, and I don’t yet know how to provide more than a single time series datapoint to HA. This might lead to some inaccurate charts.

I’ll leave this actual vs. estimated thing alone for now and focus on getting a functioning component. Maybe we could provide a separate metric for ESTIMATED points? I’m really not sure how often they show up.

3 Likes

Got it working, with some hard-coded credentials. Looks like there’s a bit of work to update the existing code to conform to recent HASS standards, but the official docs are lacking or I haven’t found the right ones yet. I’ll analyze some other integrations for hints.

Could you upload your work to GitHub @emcniece? I have some experience building modern HASS integrations, so I could take a look and help get it up to standard.

Awesome, thanks for the help @coolTNT! I’d like to, but I’m having trouble figuring out how to properly instantiate a sensor class with secrets. I’ll push as soon as I get that figured out… do you have any examples on hand?

If you can even just make a copy and strip out your secrets, I’m sure @coolTNT can help!

1 Like

Awesome to see some work on this project! Looking forward to this!

1 Like

Looking forward to this. Glad to see some BC locals actively using Home Assistant.

1 Like

Thanks for reviving this!!!

1 Like

Hey @coolTNT here’s a functional WIP of the BCHydro integration: https://github.com/emcniece/hass-bchydro

I’m not sure how to get secrets of any kind into the sensor yet, and I’m not sure if it’s best to instantiate the API in the setup_platform step or what.

It would be cool to add more sensors for the other data provided by this API - some immediately useful additional sensors would be days_since_billing, consumption_to_date, cost_to_date, estimated_consumption, and estimated_cost. It looks like we could implement clones of BCHydroUsageSensor, is that right? If so, offloading the login and API requests to a single api instance would reduce API requests, easy enough to do.

It’s late and I can’t get a response out of my server after throttling the SCAN_INTERVAL so I don’t know if this is actually working yet… will check back tomorrow morning. Thank you @coolTNT and anyone else for any insight you can offer!

Just merged a pull request for creating multiple sensors: https://github.com/emcniece/hass-bchydro/pull/2

  • Latest Usage
  • Consumption to Date
  • Cost to Date
  • Next Billing Period

This sensor.py can be used right now if you edit & manually insert your username and password. That sucks though, so I’m focusing on implementing secrets next.

1 Like