From 30ef092d58e8a8d882a6e09f8eeff70f68a731c7 Mon Sep 17 00:00:00 2001 From: Adam Pasztor Date: Tue, 24 Nov 2026 16:03:26 +0100 Subject: [PATCH] Set up the ADS integration asynchronously (#194194) --- homeassistant/components/ads/__init__.py | 16 ++++++++----- homeassistant/components/ads/binary_sensor.py | 6 ++--- homeassistant/components/ads/cover.py | 6 ++--- homeassistant/components/ads/light.py | 6 ++--- homeassistant/components/ads/select.py | 6 ++--- homeassistant/components/ads/sensor.py | 6 ++--- homeassistant/components/ads/services.py | 23 ++++++++----------- homeassistant/components/ads/switch.py | 6 ++--- homeassistant/components/ads/valve.py | 6 ++--- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/ads/__init__.py b/homeassistant/components/ads/__init__.py index c2a64dda133e..0752788ba26f 100644 --- a/homeassistant/components/ads/__init__.py +++ b/homeassistant/components/ads/__init__.py @@ -11,13 +11,13 @@ from homeassistant.const import ( CONF_PORT, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import Event, HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from .const import DATA_ADS, DOMAIN from .hub import AdsHub -from .services import setup_services +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -36,9 +36,9 @@ CONFIG_SCHEMA = probatio.Schema( ) -def setup(hass: HomeAssistant, config: ConfigType) -> bool: +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the ADS component.""" - setup_services(hass) + async_setup_services(hass) conf = config[DOMAIN] @@ -49,7 +49,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: client = pyads.Connection(net_id, port, ip_address) try: - ads = AdsHub(client) + ads = await hass.async_add_executor_job(AdsHub, client) except pyads.ADSError: _LOGGER.error( "Could not connect to ADS host (netid=%s, ip=%s, port=%s)", @@ -60,6 +60,10 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: return False hass.data[DATA_ADS] = ads - hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown) + + async def _async_shutdown(event: Event) -> None: + await hass.async_add_executor_job(ads.shutdown) + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown) return True diff --git a/homeassistant/components/ads/binary_sensor.py b/homeassistant/components/ads/binary_sensor.py index 26e3cc637d5f..c77d412ed654 100644 --- a/homeassistant/components/ads/binary_sensor.py +++ b/homeassistant/components/ads/binary_sensor.py @@ -31,10 +31,10 @@ PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Binary Sensor platform for ADS.""" @@ -45,7 +45,7 @@ def setup_platform( device_class: BinarySensorDeviceClass | None = config.get(CONF_DEVICE_CLASS) ads_sensor = AdsBinarySensor(ads_hub, name, ads_var, device_class) - add_entities([ads_sensor]) + async_add_entities([ads_sensor]) class AdsBinarySensor(AdsEntity, BinarySensorEntity): diff --git a/homeassistant/components/ads/cover.py b/homeassistant/components/ads/cover.py index 381c07e04c62..7428321c2c91 100644 --- a/homeassistant/components/ads/cover.py +++ b/homeassistant/components/ads/cover.py @@ -47,10 +47,10 @@ PLATFORM_SCHEMA = COVER_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the cover platform for ADS.""" @@ -65,7 +65,7 @@ def setup_platform( name: str = config[CONF_NAME] device_class: CoverDeviceClass | None = config.get(CONF_DEVICE_CLASS) - add_entities( + async_add_entities( [ AdsCover( ads_hub, diff --git a/homeassistant/components/ads/light.py b/homeassistant/components/ads/light.py index 345ae0b03e2d..80f5014bd83d 100644 --- a/homeassistant/components/ads/light.py +++ b/homeassistant/components/ads/light.py @@ -45,10 +45,10 @@ PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the light platform for ADS.""" @@ -61,7 +61,7 @@ def setup_platform( max_color_temp_kelvin: int | None = config.get(CONF_MAX_COLOR_TEMP_KELVIN) name: str = config[CONF_NAME] - add_entities( + async_add_entities( [ AdsLight( ads_hub, diff --git a/homeassistant/components/ads/select.py b/homeassistant/components/ads/select.py index c78a489ca613..460127721234 100644 --- a/homeassistant/components/ads/select.py +++ b/homeassistant/components/ads/select.py @@ -32,10 +32,10 @@ PLATFORM_SCHEMA = SELECT_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up an ADS select device.""" @@ -47,7 +47,7 @@ def setup_platform( entity = AdsSelect(ads_hub, ads_var, name, options) - add_entities([entity]) + async_add_entities([entity]) class AdsSelect(AdsEntity, SelectEntity): diff --git a/homeassistant/components/ads/sensor.py b/homeassistant/components/ads/sensor.py index daa260edbec2..15c2a134761d 100644 --- a/homeassistant/components/ads/sensor.py +++ b/homeassistant/components/ads/sensor.py @@ -64,10 +64,10 @@ PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up an ADS sensor device.""" @@ -92,7 +92,7 @@ def setup_platform( unit_of_measurement, ) - add_entities([entity]) + async_add_entities([entity]) class AdsSensor(AdsEntity, SensorEntity): diff --git a/homeassistant/components/ads/services.py b/homeassistant/components/ads/services.py index b30570aee3a3..c10d467362ab 100644 --- a/homeassistant/components/ads/services.py +++ b/homeassistant/components/ads/services.py @@ -1,18 +1,14 @@ """Services for the ADS integration.""" from enum import StrEnum -import logging import probatio -import pyads -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.helpers import config_validation as cv from .const import ADS_TYPEMAP, DATA_ADS, DOMAIN, AdsType -_LOGGER = logging.getLogger(__name__) - class AdsService(StrEnum): """Store keys for ADS services.""" @@ -37,24 +33,25 @@ SCHEMA_SERVICE_WRITE_DATA_BY_NAME = probatio.Schema( ) -def _handle_write_data_by_name(call: ServiceCall) -> None: +async def _async_handle_write_data_by_name(call: ServiceCall) -> None: """Write a value to the connected ADS device.""" ads = call.hass.data[DATA_ADS] ads_var: str = call.data[AdsServiceArgument.ADSVAR] ads_type: AdsType = call.data[AdsServiceArgument.ADSTYPE] value: int = call.data[AdsServiceArgument.VALUE] - try: - ads.write_by_name(ads_var, value, ADS_TYPEMAP[ads_type]) - except pyads.ADSError as err: - _LOGGER.error(err) + # The hub logs a write the PLC refuses instead of raising it. + await call.hass.async_add_executor_job( + ads.write_by_name, ads_var, value, ADS_TYPEMAP[ads_type] + ) -def setup_services(hass: HomeAssistant) -> None: +@callback +def async_setup_services(hass: HomeAssistant) -> None: """Register the ADS services.""" - hass.services.register( + hass.services.async_register( DOMAIN, AdsService.WRITE_DATA_BY_NAME, - _handle_write_data_by_name, + _async_handle_write_data_by_name, schema=SCHEMA_SERVICE_WRITE_DATA_BY_NAME, ) diff --git a/homeassistant/components/ads/switch.py b/homeassistant/components/ads/switch.py index bc20b4795641..4345c2dae558 100644 --- a/homeassistant/components/ads/switch.py +++ b/homeassistant/components/ads/switch.py @@ -28,10 +28,10 @@ PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up switch platform for ADS.""" @@ -40,7 +40,7 @@ def setup_platform( name: str = config[CONF_NAME] ads_var: str = config[CONF_ADS_VAR] - add_entities([AdsSwitch(ads_hub, name, ads_var)]) + async_add_entities([AdsSwitch(ads_hub, name, ads_var)]) class AdsSwitch(AdsEntity, SwitchEntity): diff --git a/homeassistant/components/ads/valve.py b/homeassistant/components/ads/valve.py index 2bb510fb1c2c..e2c2c65d1d7c 100644 --- a/homeassistant/components/ads/valve.py +++ b/homeassistant/components/ads/valve.py @@ -33,10 +33,10 @@ PLATFORM_SCHEMA = VALVE_PLATFORM_SCHEMA.extend( ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up an ADS valve device.""" @@ -48,7 +48,7 @@ def setup_platform( entity = AdsValve(ads_hub, ads_var, name, device_class) - add_entities([entity]) + async_add_entities([entity]) class AdsValve(AdsEntity, ValveEntity):