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

No entities created after logging in #7

Closed cooperaj closed 3 years ago

cooperaj commented 3 years ago

After setting up the integration I get no new entities. My logs contains

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 187, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 147, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/octopus_energy/__init__.py", line 47, in async_update_data
    current_agreement = get_active_agreement(point["agreements"])
  File "/config/custom_components/octopus_energy/utils.py", line 8, in get_active_agreement
    valid_to = as_utc(parse_datetime(agreement["valid_to"]))
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 133, in parse_datetime
    return ciso8601.parse_datetime(dt_str)
TypeError: argument 1 must be str, not None

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/octopus_energy/sensor.py", line 33, in async_setup_entry
    await async_setup_default_sensors(hass, entry, async_add_entities)
  File "/config/custom_components/octopus_energy/sensor.py", line 42, in async_setup_default_sensors
    await coordinator.async_config_entry_first_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 161, in async_config_entry_first_refresh
    raise ex
homeassistant.exceptions.ConfigEntryNotReady: argument 1 must be str, not None

Running a manual curl query at the url that provides the 'point' dict (in async_get_account) I get a blank response. I ran curl -u "sk_live_muhkey:" "https://api.octopus.energy/v1/accounts/A-MUHACC" and get back nothing. Looking at the API documentation I can't even seen a GET /v1/accounts endpoint. There is a POST one but that is for partner organisations only.

BottlecapDave commented 3 years ago

Ah I see the issue. I think you're on an tarrif with no current end date, whereas I'm assuming there's always one. I'll rectify this hopefully tonight.

cooperaj commented 3 years ago

Think i've cracked the empty response to curl -u "sk_live_muhkey:" "https://api.octopus.energy/v1/accounts/A-MUHACC" as curl -u "sk_live_muhkey:" "https://api.octopus.energy/v1/accounts/A-MUHACC/" brings back everything I expect (note the closing slash). I expect curl does not follow the redirect but the python library does.

I think this would still hit the issue you've described as the 'valid_to' field is null in my response.

cooperaj commented 3 years ago
from homeassistant.util.dt import (utcnow, as_utc, parse_datetime)
import datetime as dt

def get_active_agreement(agreements):
  now = utcnow()

  for agreement in agreements:
    valid_from = as_utc(parse_datetime(agreement["valid_from"]))

    if agreement["valid_to"] is not None:
      valid_to = as_utc(parse_datetime(agreement["valid_to"]))
    else:
      valid_to = now + dt.timedelta(seconds=1)

    if valid_from <= now and valid_to >= now:
      return agreement

  return None

Python is not my thing but this appears to work.

BottlecapDave commented 3 years ago

Yup that's the area. I've updated to handle it slightly differently to how you did it, just because I don't want to make any assumptions. Closing issue as I've applied a fix, but feel free to reopen if my fix is somehow still broken

cooperaj commented 3 years ago

Ah, that's the path I took originally but I think won't work. The valid_to field will always exist, but the value may either be None or a string. I think (unless my knowledge of Python is way off) the altered code will have the same issue as reported.

BottlecapDave commented 3 years ago

I think it should be fine, but to be on the safe side I've added the additional check you had as well.