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

config_entry assignation is deprecated #79

Merged

Conversation

Daemo00
Copy link
Contributor

@Daemo00 Daemo00 commented Jan 12, 2025

Fix #78.

@Daemo00 Daemo00 marked this pull request as ready for review January 12, 2025 21:37
@virtualdj
Copy link
Owner

Grazie, però vorrei poter gestire anche le versioni precedenti. La modifica fatta così com'è sarebbe compatibile solo da HA 2024.12 in poi, sulle altre invece:

ConfigFlow error

2025-01-13 18:28:00.936 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/srv/ha4/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/headers.py", line 31, in headers_middleware
    response = await handler(request)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/view.py", line 148, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/decorators.py", line 63, in with_admin
    return await func(self, request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/config/config_entries.py", line 213, in post
    return await super().post(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/components/http/data_validator.py", line 72, in wrapper
    result = await method(view, request, data, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/helpers/data_entry_flow.py", line 71, in post
    result = await self._flow_mgr.async_init(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/data_entry_flow.py", line 270, in async_init
    result = await self._async_handle_step(flow, flow.init_step, data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/ha4/lib/python3.11/site-packages/homeassistant/data_entry_flow.py", line 394, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.homeassistant/custom_components/pun_sensor/config_flow.py", line 42, in async_step_init
    default=self.config_entry.options.get(
            ^^^^^^^^^^^^^^^^^
AttributeError: 'PUNOptionsFlow' object has no attribute 'config_entry'

@virtualdj virtualdj added bug Something isn't working minor New minor version labels Jan 13, 2025
@@ -79,7 +75,7 @@ def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> PUNOptionsFlow:
"""Ottiene le opzioni per questa configurazione."""
return PUNOptionsFlow(config_entry)
return PUNOptionsFlow()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questo dovrebbe essere eseguito solo con versioni di Home Assistant >= 2024.12, mentre il vecchio codice negli altri casi (sperando che Home Assistant non si lamenti).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giusto, grazie, puoi vedere se ora va meglio?

@Daemo00 Daemo00 force-pushed the deprecated_config_entry_assignation branch from 9af437d to fcbd8a3 Compare January 16, 2025 12:06
@Daemo00 Daemo00 requested a review from virtualdj January 16, 2025 12:16
@@ -79,7 +75,11 @@ def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> PUNOptionsFlow:
"""Ottiene le opzioni per questa configurazione."""
return PUNOptionsFlow(config_entry)
if AwesomeVersion(HA_VERSION) >= AwesomeVersion("2024.12.0b0"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

È sufficiente "2024.12.0".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ho messo anche la versione beta perché il commit home-assistant/core@6d561a9 è anche lì:
image
Dici che è un problema lasciarlo?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ma dubito che qualcuno usi le beta "in produzione", perciò mi sembrava più pulito "2024.12.0".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certo, sono d'accordo che "2024.12.0" sarebbe più pulito ma non credo sarebbe corretto perché nella versione "2024.12.0b0" causerebbe l'errore che hai riportato in #79 (comment):

...
AttributeError: 'PUNOptionsFlow' object has no attribute 'config_entry'

custom_components/pun_sensor/config_flow.py Outdated Show resolved Hide resolved
@Daemo00 Daemo00 requested a review from virtualdj January 16, 2025 17:56
@Daemo00 Daemo00 force-pushed the deprecated_config_entry_assignation branch 2 times, most recently from edd5152 to a16d200 Compare January 17, 2025 10:30
@Daemo00 Daemo00 marked this pull request as draft January 17, 2025 10:37
@Daemo00 Daemo00 force-pushed the deprecated_config_entry_assignation branch from a16d200 to 78a3ddd Compare January 17, 2025 10:43
@Daemo00 Daemo00 marked this pull request as ready for review January 17, 2025 10:53
Copy link
Owner

@virtualdj virtualdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solo cambiamenti "cosmetici" ma mi piacerebbe di più così.

if AwesomeVersion(HA_VERSION) < AwesomeVersion("2024.12.0b0"):
options_flow = PUNOptionsFlow(config_entry)
else:
options_flow = PUNOptionsFlow()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puoi ritornare direttamente con return PUNOptionsFlow()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certo, fatto.
Di solito cerco di mantenere un solo punto di uscita delle funzioni/metodi però è solo una mia preferenza 😄

@@ -79,7 +82,11 @@ def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> PUNOptionsFlow:
"""Ottiene le opzioni per questa configurazione."""
return PUNOptionsFlow(config_entry)
if AwesomeVersion(HA_VERSION) < AwesomeVersion("2024.12.0b0"):
options_flow = PUNOptionsFlow(config_entry)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puoi ritornare direttamente con return PUNOptionsFlow(config_entry)?

Otherwise the following warning is raised:
Logger: homeassistant.helpers.frame
Source: helpers/frame.py:324
First occurred: January 11, 2025 at 23:29:37 (4 occurrences)
Last logged: 00:16:34

Detected that custom integration 'pun_sensor' sets option flow config_entry explicitly, which is deprecated at custom_components/pun_sensor/config_flow.py, line 33: self.config_entry = entry. This will stop working in Home Assistant 2025.12, please create a bug report at https://github.com/virtualdj/pun_sensor/issues

Deprecation happened in home-assistant/core@6d561a9
@Daemo00 Daemo00 force-pushed the deprecated_config_entry_assignation branch from 78a3ddd to 77d589c Compare January 17, 2025 17:28
@virtualdj
Copy link
Owner

virtualdj commented Jan 17, 2025

@Daemo00 Tra l'altro stavo cercando di aggiornare HACS all'ultima versione sul mio sistema di test e, tra le novità, ho trovato questa.

Pare che la modifica da fare sia ancora più semplice... prova a togliere tutto e cambiare solo:

def __init__(self, entry: config_entries.ConfigEntry) -> None:
"""Inizializzazione opzioni."""
self.config_entry = entry

in:

    def __init__(self, entry: config_entries.ConfigEntry) -> None:
        """Inizializzazione opzioni."""
        if AwesomeVersion(HA_VERSION) < AwesomeVersion("2024.12.0b0"):
            self.config_entry = entry

Dovrebbe funzionare ed è ancora più pulito! Modifica e prova sul tuo recente che io provo sul mio.

virtualdj added a commit that referenced this pull request Jan 18, 2025
Questa commit annulla le modifiche della PR #79 e applica un nuovo fix. Grazie a @Daemo00 per il contributo iniziale.
Co-authored-by: Simone Rubino <[email protected]>
This reverts commit 77d589c to apply a different fix.
Questa commit annulla le modifiche della PR virtualdj#79 e applica un nuovo fix. Grazie a @Daemo00 per il contributo iniziale.
@virtualdj virtualdj merged commit cc90530 into virtualdj:master Jan 18, 2025
3 checks passed
@Daemo00
Copy link
Contributor Author

Daemo00 commented Jan 19, 2025

@Daemo00 Tra l'altro stavo cercando di aggiornare HACS all'ultima versione sul mio sistema di test e, tra le novità, ho trovato questa.

Pare che la modifica da fare sia ancora più semplice... prova a togliere tutto e cambiare solo:

def __init__(self, entry: config_entries.ConfigEntry) -> None:
"""Inizializzazione opzioni."""
self.config_entry = entry

in:

    def __init__(self, entry: config_entries.ConfigEntry) -> None:
        """Inizializzazione opzioni."""
        if AwesomeVersion(HA_VERSION) < AwesomeVersion("2024.12.0b0"):
            self.config_entry = entry

Dovrebbe funzionare ed è ancora più pulito! Modifica e prova sul tuo recente che io provo sul mio.

Grazie che hai portato avanti il tutto, purtroppo questo weekend non ho potuto fare nulla.

@virtualdj
Copy link
Owner

@Daemo00 Nessun problema, grazie a te!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working minor New minor version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sets option flow config_entry explicitly, which is deprecated
2 participants