timmo001 / ovoenergy

Get energy data from OVO's API.
https://timmo.dev
Apache License 2.0
30 stars 16 forks source link

HA integration produces stack trace when ovoenergy.py has an error caused by missing OVO data #10

Closed RogerSelwyn closed 3 years ago

RogerSelwyn commented 3 years ago

Description

The component does not gracefully handle missing data from OVO. Previously I noticed the HA integration error when consumption data was missing, currently it is erroring because the meter readings are missing. Whilst I would expect an error to be logged, ideally we shouldn't get a stack trace as well. Being perfect, it should produce one error and then no more until the problem is resolved by OVO when it should return a resolved type information message.

How to reproduce the issue (if applicable)

Run the integration now, OVO may resolve the issue soon :-)

Screenshots (if applicable)

Error log

2021-04-23 08:59:17 ERROR (MainThread) [homeassistant.components.ovo_energy] Unexpected error fetching sensor data: 'meterReadings'
Traceback (most recent call last):
  File "/workspaces/home-assistant/homeassistant/helpers/update_coordinator.py", line 180, in _async_refresh
    self.data = await self._async_update_data()
  File "/workspaces/home-assistant/homeassistant/helpers/update_coordinator.py", line 140, in _async_update_data
    return await self.update_method()
  File "/workspaces/home-assistant/homeassistant/components/ovo_energy/__init__.py", line 55, in async_update_data
    return await client.get_daily_usage(datetime.utcnow().strftime("%Y-%m"))
  File "/usr/local/lib/python3.8/site-packages/ovoenergy/ovoenergy.py", line 100, in get_daily_usage
    usage["meterReadings"]["start"],
KeyError: 'meterReadings'

Postman extract

{
    "electricity": {
        "data": [
            {
                "consumption": 0.047,
                "interval": {
                    "start": "2021-04-18T00:00:00.000",
                    "end": "2021-04-18T00:29:59.999"
                },
                "unit": "kwh"
            },
            {
                "consumption": 0.044,
                "interval": {
                    "start": "2021-04-18T00:30:00.000",
                    "end": "2021-04-18T00:59:59.999"
                },
                "unit": "kwh"
            },

Additional information

timmo001 commented 3 years ago

Fixed in #11