Improve type hints in ads (#125825)

* Improve type hints in ads

* One more

* Adjust
This commit is contained in:
epenet 2024-09-12 15:33:27 +02:00 committed by GitHub
parent 1a478bd78a
commit e27cee53a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 24 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE
from .entity import AdsEntity from .entity import AdsEntity
from .hub import AdsHub
DEFAULT_NAME = "ADS binary sensor" DEFAULT_NAME = "ADS binary sensor"
PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
@ -50,7 +51,13 @@ def setup_platform(
class AdsBinarySensor(AdsEntity, BinarySensorEntity): class AdsBinarySensor(AdsEntity, BinarySensorEntity):
"""Representation of ADS binary sensors.""" """Representation of ADS binary sensors."""
def __init__(self, ads_hub, name, ads_var, device_class): def __init__(
self,
ads_hub: AdsHub,
name: str,
ads_var: str,
device_class: BinarySensorDeviceClass | None,
) -> None:
"""Initialize ADS binary sensor.""" """Initialize ADS binary sensor."""
super().__init__(ads_hub, name, ads_var) super().__init__(ads_hub, name, ads_var)
self._attr_device_class = device_class or BinarySensorDeviceClass.MOVING self._attr_device_class = device_class or BinarySensorDeviceClass.MOVING

View File

@ -23,6 +23,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE
from .entity import AdsEntity from .entity import AdsEntity
from .hub import AdsHub
DEFAULT_NAME = "ADS Cover" DEFAULT_NAME = "ADS Cover"
@ -57,7 +58,7 @@ def setup_platform(
"""Set up the cover platform for ADS.""" """Set up the cover platform for ADS."""
ads_hub = hass.data[DATA_ADS] ads_hub = hass.data[DATA_ADS]
ads_var_is_closed: str | None = config.get(CONF_ADS_VAR) ads_var_is_closed: str = config[CONF_ADS_VAR]
ads_var_position: str | None = config.get(CONF_ADS_VAR_POSITION) ads_var_position: str | None = config.get(CONF_ADS_VAR_POSITION)
ads_var_pos_set: str | None = config.get(CONF_ADS_VAR_SET_POS) ads_var_pos_set: str | None = config.get(CONF_ADS_VAR_SET_POS)
ads_var_open: str | None = config.get(CONF_ADS_VAR_OPEN) ads_var_open: str | None = config.get(CONF_ADS_VAR_OPEN)
@ -88,16 +89,16 @@ class AdsCover(AdsEntity, CoverEntity):
def __init__( def __init__(
self, self,
ads_hub, ads_hub: AdsHub,
ads_var_is_closed, ads_var_is_closed: str,
ads_var_position, ads_var_position: str | None,
ads_var_pos_set, ads_var_pos_set: str | None,
ads_var_open, ads_var_open: str | None,
ads_var_close, ads_var_close: str | None,
ads_var_stop, ads_var_stop: str | None,
name, name: str,
device_class, device_class: CoverDeviceClass | None,
): ) -> None:
"""Initialize AdsCover entity.""" """Initialize AdsCover entity."""
super().__init__(ads_hub, name, ads_var_is_closed) super().__init__(ads_hub, name, ads_var_is_closed)
if self._attr_unique_id is None: if self._attr_unique_id is None:

View File

@ -3,10 +3,12 @@
import asyncio import asyncio
from asyncio import timeout from asyncio import timeout
import logging import logging
from typing import Any
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from .const import STATE_KEY_STATE from .const import STATE_KEY_STATE
from .hub import AdsHub
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -16,19 +18,23 @@ class AdsEntity(Entity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, ads_hub, name, ads_var): def __init__(self, ads_hub: AdsHub, name: str, ads_var: str) -> None:
"""Initialize ADS binary sensor.""" """Initialize ADS binary sensor."""
self._state_dict = {} self._state_dict: dict[str, Any] = {}
self._state_dict[STATE_KEY_STATE] = None self._state_dict[STATE_KEY_STATE] = None
self._ads_hub = ads_hub self._ads_hub = ads_hub
self._ads_var = ads_var self._ads_var = ads_var
self._event = None self._event: asyncio.Event | None = None
self._attr_unique_id = ads_var self._attr_unique_id = ads_var
self._attr_name = name self._attr_name = name
async def async_initialize_device( async def async_initialize_device(
self, ads_var, plctype, state_key=STATE_KEY_STATE, factor=None self,
): ads_var: str,
plctype: type,
state_key: str = STATE_KEY_STATE,
factor: int | None = None,
) -> None:
"""Register device notification.""" """Register device notification."""
def update(name, value): def update(name, value):

View File

@ -21,6 +21,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE
from .entity import AdsEntity from .entity import AdsEntity
from .hub import AdsHub
CONF_ADS_VAR_BRIGHTNESS = "adsvar_brightness" CONF_ADS_VAR_BRIGHTNESS = "adsvar_brightness"
STATE_KEY_BRIGHTNESS = "brightness" STATE_KEY_BRIGHTNESS = "brightness"
@ -54,7 +55,13 @@ def setup_platform(
class AdsLight(AdsEntity, LightEntity): class AdsLight(AdsEntity, LightEntity):
"""Representation of ADS light.""" """Representation of ADS light."""
def __init__(self, ads_hub, ads_var_enable, ads_var_brightness, name): def __init__(
self,
ads_hub: AdsHub,
ads_var_enable: str,
ads_var_brightness: str | None,
name: str,
) -> None:
"""Initialize AdsLight entity.""" """Initialize AdsLight entity."""
super().__init__(ads_hub, name, ads_var_enable) super().__init__(ads_hub, name, ads_var_enable)
self._state_dict[STATE_KEY_BRIGHTNESS] = None self._state_dict[STATE_KEY_BRIGHTNESS] = None

View File

@ -45,11 +45,8 @@ def setup_platform(
ads_var: str = config[CONF_ADS_VAR] ads_var: str = config[CONF_ADS_VAR]
name: str = config[CONF_NAME] name: str = config[CONF_NAME]
device_class: ValveDeviceClass | None = config.get(CONF_DEVICE_CLASS) device_class: ValveDeviceClass | None = config.get(CONF_DEVICE_CLASS)
supported_features: ValveEntityFeature = (
ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE
)
entity = AdsValve(ads_hub, ads_var, name, device_class, supported_features) entity = AdsValve(ads_hub, ads_var, name, device_class)
add_entities([entity]) add_entities([entity])
@ -57,18 +54,18 @@ def setup_platform(
class AdsValve(AdsEntity, ValveEntity): class AdsValve(AdsEntity, ValveEntity):
"""Representation of an ADS valve entity.""" """Representation of an ADS valve entity."""
_attr_supported_features = ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE
def __init__( def __init__(
self, self,
ads_hub: AdsHub, ads_hub: AdsHub,
ads_var: str, ads_var: str,
name: str, name: str,
device_class: ValveDeviceClass | None, device_class: ValveDeviceClass | None,
supported_features: ValveEntityFeature,
) -> None: ) -> None:
"""Initialize AdsValve entity.""" """Initialize AdsValve entity."""
super().__init__(ads_hub, name, ads_var) super().__init__(ads_hub, name, ads_var)
self._attr_device_class = device_class self._attr_device_class = device_class
self._attr_supported_features = supported_features
self._attr_reports_position = False self._attr_reports_position = False
self._attr_is_closed = True self._attr_is_closed = True