Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mfa token config flow #11

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft

Conversation

shtrom
Copy link

@shtrom shtrom commented Jul 2, 2024

Based on #9, fixes #10.

  • add a ConfigFlow module, so the token can be renewed in the UI, along with nice notifications when it gets outdated
  • bump version to v2.0.0 due to breaking change in the entity names (addition of the service_agreement_id to support multiple accounts, but needing re-adding to the energy dashboard), and the change in configuration method.

shtrom added 24 commits May 30, 2023 20:49
Signed-off-by: Olivier Mehani <[email protected]>
Tariffs 31, 41, 61 and 62 are a wild guess, and totally untested.

fixes: LeighCurran#6 some more
Signed-off-by: Olivier Mehani <[email protected]>
Neither STATE_CLASS_TOTAL_INCREASING nor STATE_CLASS_MEASUREMENT work
well for the ca. 9am reset that seems to happen for daily summary data.

The `last_reset` time is set to whenever a value changes. This means
this is liable to a bug where a value will be missed it it is the same
as the previous day.

Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
The AuroraApi proxies everything to the session, or swallows the error
transparently.  This is not the behaviour that the Throttle wrapper
expects, as it needs to use a _throttle attribute on the wrapped object.

We add a special case in the proxy juuuuust for this.

Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Waiting for LeighCurran/AuroraPlus#1 to be merged.

Signed-off-by: Olivier Mehani <[email protected]>
This is useful for the T140 feed-in, which is negative, when HA expect a
positive return value.

Signed-off-by: Olivier Mehani <[email protected]>
Usage:

1. obtain an `access_token` following instructions at [0]
2. update the secrets to use an empty username (`''`), and the
   `access_token` as the password

[0] https://github.com/shtrom/AuroraPlus/tree/oauth-mfa-token\#obtain-a-token

Signed-off-by: Olivier Mehani <[email protected]>
Fixes

    Traceback (most recent call last):
    File /usr/src/homeassistant/homeassistant/loader.py, line 842, in get_platform
    cache[full_name] = self._import_platform(platform_name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/src/homeassistant/homeassistant/loader.py, line 859, in _import_platform
    return importlib.import_module(f{self.pkg_path}.{platform_name})
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/local/lib/python3.11/importlib/init.py, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File , line 1204, in _gcd_import
    File , line 1176, in _find_and_load
    File , line 1147, in _find_and_load_unlocked
    File , line 690, in _load_unlocked
    File , line 940, in exec_module
    File , line 241, in _call_with_frames_removed
    File /config/custom_components/auroraplus/sensor.py, line 39, in
    from homeassistant_historical_sensor import (
    File /usr/local/lib/python3.11/site-packages/homeassistant_historical_sensor/init.py, line 39, in
    raise SystemError(msg)
    SystemError: Running HomeAssistant 2024.1.2, Minimum required version >=2023.6.0

See [0], [1]

[0] ldotlopez/ha-historical-sensor#12
[1] https://github.com/LeighCurran/AuroraPlus/pull/3\#issuecomment-1884213483

Signed-off-by: Olivier Mehani <[email protected]>
Nicer entities names

Update README for Config Flow

Move AuroraApi to... api

Signed-off-by: Olivier Mehani <[email protected]>
@shtrom shtrom marked this pull request as draft July 2, 2024 11:36
@shtrom
Copy link
Author

shtrom commented Jul 2, 2024

@LeighCurran I thought I had this PR around before, but can't find it. I now added a ConfigFlow module, so the token can be renewed in the UI, along with nice notifications when it gets outdated. It's updating to v2.0.0 due to breaking change in the entity names (needing re-adding to the energy dashboard), and the change in configuration method (for which I haven't added a migration step).

@slothking87 wanna give it a try, if you're game?

@shtrom
Copy link
Author

shtrom commented Jul 3, 2024

There is a persistent, but seemingly harmless (EDIT: it looks like no data comes is updated until a restart...), bug that occurs after updating the access_token, and continues until HA is restarted.

homeassistant  | 2024-07-03 12:03:49.098 ERROR (Recorder) [homeassistant.components.recorder.core] SQLAlchemyError error processing task CommitTask()                   
homeassistant  | Traceback (most recent call last):                                                                                                                     
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/recorder/core.py", line 926, in _process_one_task_or_event_or_recover                          
homeassistant  |     task.run(self)                                                                                                                                     
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/recorder/tasks.py", line 291, in run                                                           
homeassistant  |     instance._commit_event_session_or_retry()  # noqa: SLF001                                                                                          
homeassistant  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                          
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/recorder/core.py", line 1191, in _commit_event_session_or_retry                                
homeassistant  |     self._commit_event_session()                                                                                                                       
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/recorder/core.py", line 1217, in _commit_event_session                                         
homeassistant  |     session.execute(                                                                                                                                   
homeassistant  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2351, in execute                                                      
homeassistant  |     return self._execute_internal(                                                                                                                     
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2236, in _execute_internal
homeassistant  |     result: Result[Any] = compile_state_cls.orm_execute_statement(
homeassistant  |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/bulk_persistence.py", line 1603, in orm_execute_statement
homeassistant  |     result = _bulk_update(
homeassistant  |              ^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/bulk_persistence.py", line 335, in _bulk_update
homeassistant  |     persistence._emit_update_statements(
homeassistant  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 948, in _emit_update_statements
homeassistant  |     raise orm_exc.StaleDataError(
homeassistant  | sqlalchemy.orm.exc.StaleDataError: UPDATE statement on table 'states' expected to update 1 row(s); 0 were matched.

I haven't gotten to the bottom of it yet, but I suspect this has to do with using the historical sensor, and HA trying to update the current (non-existent) reading. @ldotlopez, perhaps you have seen a similar issue before (the issue also happens with 2.0.0rc6)?

@ldotlopez
Copy link

ldotlopez commented Jul 5, 2024

I haven't gotten to the bottom of it yet, but I suspect this has to do with using the historical sensor, and HA trying to update the current (non-existent) reading. @ldotlopez, perhaps you have seen a similar issue before (the issue also happens with 2.0.0rc6)?

@shtrom Yes, I have this issue too.

For me, It surfaces when two historical sensors are enabled for the same integration. I spent so many hours in this issue that gives me headaches to think about it.

Historical-sensors consists of two main parts: handling historical states and handling (import) historical statistics.

For importing statistics H-A has an API, but it doesn't for the first one. For this reason historical-sensors manipulates the DB "directly" (using SQLalchemy and some helper functions from the recorder component). The problem is that H-A recently added some layer on top of the DB that I was unable to made work with historical states.

The only solution I can see now is to remove the handling of historical states and just transform upstream data into statistics.

@shtrom
Copy link
Author

shtrom commented Jul 6, 2024

Yep, cool, @ldotlopez. Thanks for the details!

What do you mean by “remove the handling of historical states and just transform upstream data into statistics”? This sounds like dropping the use of ha-historical-sensors? Or is this just something like disabling stats, but not raw data input? I remember struggling to get stats working, but can't recall the details.

In any case, we can't do that here, as we never get immediate data, just full-day hourly data, the next day.

It's ok, though, with a restart, the problem clears and everything works, so I'll look at a way to make the config update make HA ask for a reboot.

@slothking87
Copy link

@LeighCurran I thought I had this PR around before, but can't find it. I now added a ConfigFlow module, so the token can be renewed in the UI, along with nice notifications when it gets outdated. It's updating to v2.0.0 due to breaking change in the entity names (needing re-adding to the energy dashboard), and the change in configuration method (for which I haven't added a migration step).

@slothking87 wanna give it a try, if you're game?

Sorry - Been away and forgot to have a look.
Works perfectly as far as I can tell so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to retrieve data
3 participants