From a9d7d5a48729e25b5f3cef1d14b6ea4092205acf Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Fri, 7 Jun 2024 09:54:55 -0500 Subject: [PATCH] Move pytz file I/O to executor The astral package uses pytz which does file I/O when it sees a new time zone. Force this file I/O to happen in an executor. --- custom_components/illuminance/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/custom_components/illuminance/__init__.py b/custom_components/illuminance/__init__.py index 7a71294..457dc49 100644 --- a/custom_components/illuminance/__init__.py +++ b/custom_components/illuminance/__init__.py @@ -50,9 +50,23 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up composite integration.""" - def get_loc_elev(event: Event | None = None) -> None: + async def async_get_loc_elev(event: Event | None = None) -> None: """Get HA Location object & elevation.""" - hass.data[DOMAIN] = get_astral_location(hass) + if event is not None and not event.data: + return + + def get_loc_elev() -> None: + """Get HA Location object & elevation. + + Then get Location's tzinfo to force pytz, which it calls indirectly, to do + its file I/O that it does when it sees a new time zone. This needs to be + done in an executor. + """ + loc, elv = get_astral_location(hass) + loc.tzinfo # noqa: B018 + hass.data[DOMAIN] = loc, elv + + await hass.async_add_executor_job(get_loc_elev) async def process_config( config: ConfigType | None, run_immediately: bool = True @@ -89,10 +103,10 @@ async def reload_config(call: ServiceCall | None = None) -> None: """Reload configuration.""" await process_config(await async_integration_yaml_config(hass, DOMAIN)) - get_loc_elev() + await async_get_loc_elev() await process_config(config, run_immediately=False) async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, reload_config) - hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, get_loc_elev) + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, async_get_loc_elev) return True