Set up the ADS integration asynchronously (#194194)

This commit is contained in:
Adam Pasztor
2026-11-24 16:03:26 +01:00
committed by GitHub
parent b749b909e0
commit 30ef092d58
9 changed files with 41 additions and 40 deletions
+10 -6
View File
@@ -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
@@ -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):
+3 -3
View File
@@ -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,
+3 -3
View File
@@ -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,
+3 -3
View File
@@ -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):
+3 -3
View File
@@ -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):
+10 -13
View File
@@ -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,
)
+3 -3
View File
@@ -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):
+3 -3
View File
@@ -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):