barisdemirdelen / homeassistant-greenchoice

This is a Home Assistant custom component that connects to the Greenchoice API
MIT License
31 stars 6 forks source link

get_meter_readings doesn't return dict but list #21

Open victorclaessen opened 23 hours ago

victorclaessen commented 23 hours ago

Hi. My greenchoice sensor (v1.5.0) isn't working anymore. I get this error in the home-assistant log.

2024-11-15 21:38:02.576 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up greenchoice platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 365, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/greenchoice/sensor.py", line 107, in setup_platform
    throttled_api_update(greenchoice_api)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 184, in wrapper
    result = method(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/greenchoice/sensor.py", line 124, in throttled_api_update
    api_result = api.update()
                 ^^^^^^^^^^^^
  File "/config/custom_components/greenchoice/api.py", line 207, in update
    self.update_usage_values(self.result)
  File "/config/custom_components/greenchoice/api.py", line 223, in update_usage_values
    meter_readings = self.get_meter_readings()
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/greenchoice/api.py", line 126, in get_meter_readings
    return MeterReadings.from_dict(meter_json)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/greenchoice/model.py", line 300, in from_dict
    productTypes=[MeterProduct.from_dict(r) for r in data.get("productTypes")],
                                                     ^^^^^^^^
AttributeError: 'list' object has no attribute 'get'

I added this line in api.py at line 123 to dump the value sent from api.py to model.py:

        _LOGGER.debug(f"Result of get_meter_readings: {meter_json}")

And got this value:

2024-11-15 21:38:02.573 DEBUG (SyncWorker_9) [custom_components.greenchoice.api] Result of get_meter_readings: [{'productType': 'Stroom', 'months': [{'month': 12, 'readings': []}, {'month': 11, 'readings': [{'readingDate': '2024-11-14T00:00:00', 'normalConsumption' (snip)

So if I look for the cause: In line 300 it is indeed expecting a dictionary with a key productTypes

            productTypes=[MeterProduct.from_dict(r) for r in data.get("productTypes")],

But the value sent from api.py to model.py is a list, not a dict. And this list (of course) does not have a key "productTypes" (it does not have any keys, because it is a list).

Did the API change again?

luc-ass commented 4 hours ago

Same here. Seems like the API changed in deed.