BottlecapDave / HomeAssistant-OctopusEnergy

Unofficial Home Assistant integration for interacting with Octopus Energy
https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/
MIT License
571 stars 57 forks source link

fix:api_client:Handle 'None' smart meter consumption #779

Closed paul-ri closed 5 months ago

paul-ri commented 7 months ago

tldr; Fix ERROR (MainThread) [custom_components.octopus_energy.coordinators.current_consumption] Unexpected error fetching current_consumption_44-11-XXXXXXXXXX data: float() argument must be a string or a real number, not 'NoneType'


I had my smart meter installed today + Octopus Mini setup. I immediately looked at integrating it with Home Assistant. Whilst most of the setup went fine, Home Assistant would display the following error:

homeassistant-1  | 2024-02-27 15:44:30.872 ERROR (MainThread) [custom_components.octopus_energy.coordinators.current_consumption] Unexpected error fetching current_consumption_44-11-XXXXXXXXXX data: float() argument must be a string or a real number, not 'NoneType'
homeassistant-1  | Traceback (most recent call last):
homeassistant-1  |   File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 313, in _async_refresh
homeassistant-1  |     self.data = await self._async_update_data()
homeassistant-1  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  |   File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 269, in _async_update_data
homeassistant-1  |     return await self.update_method()
homeassistant-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  |   File "/config/custom_components/octopus_energy/coordinators/current_consumption.py", line 81, in async_update_data
homeassistant-1  |     hass.data[DOMAIN][account_id][key] = await async_get_live_consumption(
homeassistant-1  |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  |   File "/config/custom_components/octopus_energy/coordinators/current_consumption.py", line 39, in async_get_live_consumption
homeassistant-1  |     data = await client.async_get_smart_meter_consumption(device_id, period_from, period_to)
homeassistant-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  |   File "/config/custom_components/octopus_energy/api_client/__init__.py", line 660, in async_get_smart_meter_consumption
homeassistant-1  |     return list(map(lambda mp: {
homeassistant-1  |            ^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  |   File "/config/custom_components/octopus_energy/api_client/__init__.py", line 661, in <lambda>
homeassistant-1  |     "consumption": float(mp["consumptionDelta"]) / 1000,
homeassistant-1  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant-1  | TypeError: float() argument must be a string or a real number, not 'NoneType'

Handling the float() value and returning 'None' wasn't ideal as more failures would happen down the line; functions wouldn't expect to get a 'None' consumption. So it was better instead to skip any result without a consumptionDelta from the API response.

Looks like it's my gas consumption that is empty, likely due to the fact my account still lists my 2 old non-smart meters.

Testing done

Modified the code straight in my Home Assistant container, restarted it, looked at logs for 2min: OK

BottlecapDave commented 7 months ago

Hello. Sorry for the late response and thank you for looking into this issue. Please see my comments :)

BottlecapDave commented 5 months ago

Due to lack of activity, this has been resolved in the suggested way elsewhere.