Skip to content

Commit

Permalink
Fix blocking call to import_module; closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualdj committed May 3, 2024
1 parent 66d316e commit c1d3125
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ Ovviamente non ho alcuna certezza che tutto questo sia la maniera giusta di proc
- Il componente di Home Assistant [energyzero](https://github.com/home-assistant/core/tree/dev/homeassistant/components/energyzero) per il blocco del `config_flow` già configurato e per esprimere correttamente le unità di misura
- La [PR #99213](https://github.com/home-assistant/core/pull/99213/files) di Home Assistant per il suggerimento di usare `async_call_later` anziché sommare il timedelta all'ora corrente
- La [PR #76793](https://github.com/home-assistant/core/pull/76793/files) di Home Assistant per un esempio di come usare il [cancellation token](https://developers.home-assistant.io/docs/integration_listen_events/#available-event-helpers) restituito da `async_track_point_in_time`
- I commit [1](https://github.com/home-assistant/core/commit/c574d86ddbafd6c18995ad9efb297fda3ce4292c) e [2](https://github.com/home-assistant/core/commit/36e7689d139d0f517bbdd8f8f2c11e18936d27b3) per risolvere il warning nei log `[homeassistant.util.loop] Detected blocking call to import_module inside the event loop by custom integration` comparso con la versione 2024.5.0 di Home Assistant e dovuto alle librerie importate
14 changes: 12 additions & 2 deletions custom_components/pun_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bs4 import BeautifulSoup
import xml.etree.ElementTree as et
from typing import Tuple
from functools import partial

from aiohttp import ClientSession
from homeassistant.core import HomeAssistant
Expand All @@ -16,6 +17,7 @@
UpdateFailed,
)
from homeassistant.helpers.event import async_track_point_in_time, async_call_later
from homeassistant.setup import SetupPhases, async_pause_setup
import homeassistant.util.dt as dt_util
from zoneinfo import ZoneInfo

Expand Down Expand Up @@ -44,6 +46,10 @@

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
"""Impostazione dell'integrazione da configurazione Home Assistant"""

# Carica le dipendenze di holidays in background
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
await hass.async_add_import_executor_job(holidays.IT)

# Salva il coordinator nella configurazione
coordinator = PUNDataUpdateCoordinator(hass, config)
Expand Down Expand Up @@ -163,7 +169,9 @@ async def _async_update_data(self):
# Apre la pagina per generare i cookie e i campi nascosti
_LOGGER.debug('Connessione a URL login.')
async with self.session.get(LOGIN_URL) as response:
soup = BeautifulSoup(await response.read(), features='html.parser')
soup = await self.hass.async_add_executor_job(
partial(BeautifulSoup, await response.read(), features='html.parser')
)

# Recupera i campi nascosti __VIEWSTATE e __EVENTVALIDATION per la prossima richiesta
viewstate = soup.find('input',{'name':'__VIEWSTATE'})['value']
Expand All @@ -179,7 +187,9 @@ async def _async_update_data(self):
# Effettua il login (che se corretto porta alla pagina di download XML grazie al 'ReturnUrl')
_LOGGER.debug('Invio credenziali a URL login.')
async with self.session.post(LOGIN_URL, data=login_payload) as response:
soup = BeautifulSoup(await response.read(), features='html.parser')
soup = await self.hass.async_add_executor_job(
partial(BeautifulSoup, await response.read(), features='html.parser')
)

# Recupera i campi nascosti __VIEWSTATE per la prossima richiesta
viewstate = soup.find('input',{'name':'__VIEWSTATE'})['value']
Expand Down
4 changes: 3 additions & 1 deletion custom_components/pun_sensor/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"documentation": "https://github.com/virtualdj/pun_sensor",
"integration_type": "hub",
"iot_class": "cloud_polling",
"import_executor": true,
"issue_tracker": "https://github.com/virtualdj/pun_sensor/issues",
"loggers": ["pun_sensor"],
"requirements": ["holidays", "bs4"],
"version": "0.6.0"
"single_config_entry": true,
"version": "0.7.0"
}

0 comments on commit c1d3125

Please sign in to comment.