home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.14k stars 29.82k forks source link

Opower - unable to initialize and fetch account data due to bug in code #122351

Open jyl087 opened 1 month ago

jyl087 commented 1 month ago

The problem

I tried to add a Opower integration to my Home Assistant setup to access my PG&E account. I kept seeing errors during initialization so the component was never able to successfully access my account(s). Looking at the HASS log, I could see that it was actually logging in and fetching account data, but for some reason the code was generating an exception in the __insert_statistics method. The upshot was caused by the fact that the "last_update_success" property was not getting set to True. Inserted "self.last_update_success = True" in line 83 (after async_login()) in coordinator.py. This fixed the problem.

What version of Home Assistant Core has the issue?

core-2024.6.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

opower

Link to integration documentation on our website

https://www.home-assistant.io/integrations/opower/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 month ago

Hey there @tronikos, mind taking a look at this issue as it has been labeled with an integration (opower) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `opower` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign opower` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


opower documentation opower source (message by IssueLinks)

jyl087 commented 1 month ago

Just to clarify, the log below shows what was happening. The library was able to access my account stats, but the final line, "Finished fetching.... (Success: False)", was the clue that led to the fix I suggested above.

2024-07-21 09:24:14.915 DEBUG (MainThread) [homeassistant.components.opower.coordinator] Updating sensor data with: [Forecast(account=Account(customer=Customer(uuid='xxxxx'), uuid='xxxxx', utility_account_id='xxxx', id='xxxx', meter_type=<MeterType.ELEC: 'ELEC'>, read_resolution=None), start_date=datetime.date(2024, 7, 11), end_date=datetime.date(2024, 8, 9), current_date=datetime.date(2024, 7, 21), unit_of_measure=<UnitOfMeasure.KWH: 'KWH'>, usage_to_date=184.0, cost_to_date=78.0, forecasted_usage=583.0, forecasted_cost=288.0, typical_usage=382.0, typical_cost=103.0), Forecast(account=Account(customer=Customer(uuid='xxxx'), uuid='xxxx', utility_account_id='xxxx', id='xxxx', meter_type=<MeterType.GAS: 'GAS'>, read_resolution=None), start_date=datetime.date(2024, 6, 22), end_date=datetime.date(2024, 7, 21), current_date=datetime.date(2024, 7, 21), unit_of_measure=<UnitOfMeasure.THERM: 'THERM'>, usage_to_date=21.0, cost_to_date=49.0, forecasted_usage=23.0, forecasted_cost=51.0, typical_usage=17.0, typical_cost=29.0), Forecast(account=Account(customer=Customer(uuid='xxxx'), uuid='xxxx', utility_account_id='xxxx', id='xxxx', meter_type=<MeterType.ELEC: 'ELEC'>, read_resolution=None), start_date=datetime.date(2024, 7, 11), end_date=datetime.date(2024, 8, 9), current_date=datetime.date(2024, 7, 21), unit_of_measure=<UnitOfMeasure.KWH: 'KWH'>, usage_to_date=123.0, cost_to_date=52.0, forecasted_usage=519.0, forecasted_cost=228.0, typical_usage=805.0, typical_cost=352.0)]
2024-07-21 09:24:14.915 DEBUG (MainThread) [homeassistant.components.opower.coordinator] Updating Statistics for opower:pge_elec_xxxx_energy_cost and opower:pge_elec_xxxx_energy_consumption
2024-07-21 09:24:14.919 DEBUG (MainThread) [homeassistant.components.opower.coordinator] Updating statistic for the first time
2024-07-21 09:24:15.073 DEBUG (MainThread) [homeassistant.components.opower.coordinator] Finished fetching Opower data in 4.176 seconds (success: False)
tronikos commented 1 month ago

I don't think the line you added properly fixes this. You said: "for some reason the code was generating an exception in the __insert_statistics method." What was the exception?

FYI, per https://www.home-assistant.io/integrations/opower#energy you need to add the statistics and not the sensors to the energy dashboard. The line you added allowed the creation of the sensors but I assume the statistics still failed. Do you have historical data in the energy dashboard from account activation up to a couple of days ago?

jyl087 commented 1 month ago

Like I said, the exception is "Finished fetching... (Success: False)", which is due to the bug I pointed out. The oPower data is actually being retrieved, but the code mistakenly falls into the exception handler due to the bug I pointed out. After fixing the bug, my custom fork of your integration works properly. Note, I'm not asking for any further assistance; I'm merely pointing out a code change that I implemented that allowed the integration to work for me.