timmo001 / ovoenergy

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

Fix issue when there is no gas supplied by OVO #6

Closed RogerSelwyn closed 3 years ago

RogerSelwyn commented 3 years ago

Description

The following errors are logged when there is no Gas supplied by OVO, therefore the Gas object is 'None'.

2020-08-12 19:01:55 ERROR (MainThread) [homeassistant.components.ovo_energy] Unexpected error fetching sensor data: argument of type 'NoneType' is not iterable
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 143, in async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 131, in _async_update_data
    return await self.update_method()
  File "/usr/src/homeassistant/homeassistant/components/ovo_energy/__init__.py", line 43, in async_update_data
    return await client.get_daily_usage(now.strftime("%Y-%m"))
  File "/usr/local/lib/python3.8/site-packages/ovoenergy/ovoenergy.py", line 100, in get_daily_usage
    if "data" in gas:
TypeError: argument of type 'NoneType' is not iterable
2020-08-12 19:01:55 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up ovo_energy platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 184, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/ovo_energy/sensor.py", line 30, in async_setup_entry
    currency = coordinator.data.electricity[
AttributeError: 'NoneType' object has no attribute 'electricity'

Note that this code generates a number of flake8 linting errors which I have not corrected, since already present. Most relevant is this:

if response.status is not 200 should be changed to if response.status != 200

Related issues this fixes

https://github.com/home-assistant/core/issues/38805

Checklist

RogerSelwyn commented 3 years ago

Thanks, that fixed it.