Add class init type hint to xiaomi_aqara (#145255)

This commit is contained in:
epenet 2025-05-20 11:56:17 +02:00 committed by GitHub
parent c1da554eb1
commit e39c8e350c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 150 additions and 25 deletions

View File

@ -1,6 +1,9 @@
"""Support for Xiaomi aqara binary sensors.""" """Support for Xiaomi aqara binary sensors."""
import logging import logging
from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -137,7 +140,15 @@ async def async_setup_entry(
class XiaomiBinarySensor(XiaomiDevice, BinarySensorEntity): class XiaomiBinarySensor(XiaomiDevice, BinarySensorEntity):
"""Representation of a base XiaomiBinarySensor.""" """Representation of a base XiaomiBinarySensor."""
def __init__(self, device, name, xiaomi_hub, data_key, device_class, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
xiaomi_hub: XiaomiGateway,
data_key: str,
device_class: BinarySensorDeviceClass | None,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiSmokeSensor.""" """Initialize the XiaomiSmokeSensor."""
self._data_key = data_key self._data_key = data_key
self._attr_device_class = device_class self._attr_device_class = device_class
@ -152,11 +163,21 @@ class XiaomiBinarySensor(XiaomiDevice, BinarySensorEntity):
class XiaomiNatgasSensor(XiaomiBinarySensor): class XiaomiNatgasSensor(XiaomiBinarySensor):
"""Representation of a XiaomiNatgasSensor.""" """Representation of a XiaomiNatgasSensor."""
def __init__(self, device, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiSmokeSensor.""" """Initialize the XiaomiSmokeSensor."""
self._density = None self._density = None
super().__init__( super().__init__(
device, "Natgas Sensor", xiaomi_hub, "alarm", "gas", config_entry device,
"Natgas Sensor",
xiaomi_hub,
"alarm",
BinarySensorDeviceClass.GAS,
config_entry,
) )
@property @property
@ -197,7 +218,13 @@ class XiaomiNatgasSensor(XiaomiBinarySensor):
class XiaomiMotionSensor(XiaomiBinarySensor): class XiaomiMotionSensor(XiaomiBinarySensor):
"""Representation of a XiaomiMotionSensor.""" """Representation of a XiaomiMotionSensor."""
def __init__(self, device, hass, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
hass: HomeAssistant,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiMotionSensor.""" """Initialize the XiaomiMotionSensor."""
self._hass = hass self._hass = hass
self._no_motion_since = 0 self._no_motion_since = 0
@ -207,7 +234,12 @@ class XiaomiMotionSensor(XiaomiBinarySensor):
else: else:
data_key = "motion_status" data_key = "motion_status"
super().__init__( super().__init__(
device, "Motion Sensor", xiaomi_hub, data_key, "motion", config_entry device,
"Motion Sensor",
xiaomi_hub,
data_key,
BinarySensorDeviceClass.MOTION,
config_entry,
) )
@property @property
@ -295,7 +327,12 @@ class XiaomiMotionSensor(XiaomiBinarySensor):
class XiaomiDoorSensor(XiaomiBinarySensor, RestoreEntity): class XiaomiDoorSensor(XiaomiBinarySensor, RestoreEntity):
"""Representation of a XiaomiDoorSensor.""" """Representation of a XiaomiDoorSensor."""
def __init__(self, device, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiDoorSensor.""" """Initialize the XiaomiDoorSensor."""
self._open_since = 0 self._open_since = 0
if "proto" not in device or int(device["proto"][0:1]) == 1: if "proto" not in device or int(device["proto"][0:1]) == 1:
@ -356,7 +393,12 @@ class XiaomiDoorSensor(XiaomiBinarySensor, RestoreEntity):
class XiaomiWaterLeakSensor(XiaomiBinarySensor): class XiaomiWaterLeakSensor(XiaomiBinarySensor):
"""Representation of a XiaomiWaterLeakSensor.""" """Representation of a XiaomiWaterLeakSensor."""
def __init__(self, device, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiWaterLeakSensor.""" """Initialize the XiaomiWaterLeakSensor."""
if "proto" not in device or int(device["proto"][0:1]) == 1: if "proto" not in device or int(device["proto"][0:1]) == 1:
data_key = "status" data_key = "status"
@ -402,11 +444,21 @@ class XiaomiWaterLeakSensor(XiaomiBinarySensor):
class XiaomiSmokeSensor(XiaomiBinarySensor): class XiaomiSmokeSensor(XiaomiBinarySensor):
"""Representation of a XiaomiSmokeSensor.""" """Representation of a XiaomiSmokeSensor."""
def __init__(self, device, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiSmokeSensor.""" """Initialize the XiaomiSmokeSensor."""
self._density = 0 self._density = 0
super().__init__( super().__init__(
device, "Smoke Sensor", xiaomi_hub, "alarm", "smoke", config_entry device,
"Smoke Sensor",
xiaomi_hub,
"alarm",
BinarySensorDeviceClass.SMOKE,
config_entry,
) )
@property @property
@ -446,7 +498,14 @@ class XiaomiSmokeSensor(XiaomiBinarySensor):
class XiaomiVibration(XiaomiBinarySensor): class XiaomiVibration(XiaomiBinarySensor):
"""Representation of a Xiaomi Vibration Sensor.""" """Representation of a Xiaomi Vibration Sensor."""
def __init__(self, device, name, data_key, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
data_key: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiVibration.""" """Initialize the XiaomiVibration."""
self._last_action = None self._last_action = None
super().__init__(device, name, xiaomi_hub, data_key, None, config_entry) super().__init__(device, name, xiaomi_hub, data_key, None, config_entry)
@ -485,7 +544,15 @@ class XiaomiVibration(XiaomiBinarySensor):
class XiaomiButton(XiaomiBinarySensor): class XiaomiButton(XiaomiBinarySensor):
"""Representation of a Xiaomi Button.""" """Representation of a Xiaomi Button."""
def __init__(self, device, name, data_key, hass, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
data_key: str,
hass: HomeAssistant,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiButton.""" """Initialize the XiaomiButton."""
self._hass = hass self._hass = hass
self._last_action = None self._last_action = None
@ -545,7 +612,13 @@ class XiaomiButton(XiaomiBinarySensor):
class XiaomiCube(XiaomiBinarySensor): class XiaomiCube(XiaomiBinarySensor):
"""Representation of a Xiaomi Cube.""" """Representation of a Xiaomi Cube."""
def __init__(self, device, hass, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
hass: HomeAssistant,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the Xiaomi Cube.""" """Initialize the Xiaomi Cube."""
self._hass = hass self._hass = hass
self._last_action = None self._last_action = None

View File

@ -2,6 +2,8 @@
from typing import Any from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.cover import ATTR_POSITION, CoverEntity from homeassistant.components.cover import ATTR_POSITION, CoverEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -40,7 +42,14 @@ async def async_setup_entry(
class XiaomiGenericCover(XiaomiDevice, CoverEntity): class XiaomiGenericCover(XiaomiDevice, CoverEntity):
"""Representation of a XiaomiGenericCover.""" """Representation of a XiaomiGenericCover."""
def __init__(self, device, name, data_key, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
data_key: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiGenericCover.""" """Initialize the XiaomiGenericCover."""
self._data_key = data_key self._data_key = data_key
self._pos = 0 self._pos = 0

View File

@ -2,8 +2,11 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import TYPE_CHECKING, Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_VOLTAGE, CONF_MAC from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_VOLTAGE, CONF_MAC
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
@ -24,7 +27,13 @@ class XiaomiDevice(Entity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, device, device_type, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
device_type: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the Xiaomi device.""" """Initialize the Xiaomi device."""
self._is_available = True self._is_available = True
self._sid = device["sid"] self._sid = device["sid"]
@ -50,6 +59,8 @@ class XiaomiDevice(Entity):
if config_entry.data[CONF_MAC] == format_mac(self._sid): if config_entry.data[CONF_MAC] == format_mac(self._sid):
# this entity belongs to the gateway itself # this entity belongs to the gateway itself
self._is_gateway = True self._is_gateway = True
if TYPE_CHECKING:
assert config_entry.unique_id
self._device_id = config_entry.unique_id self._device_id = config_entry.unique_id
else: else:
# this entity is connected through zigbee # this entity is connected through zigbee
@ -86,6 +97,8 @@ class XiaomiDevice(Entity):
model=self._model, model=self._model,
) )
else: else:
if TYPE_CHECKING:
assert self._gateway_id is not None
device_info = DeviceInfo( device_info = DeviceInfo(
connections={(dr.CONNECTION_ZIGBEE, self._device_id)}, connections={(dr.CONNECTION_ZIGBEE, self._device_id)},
identifiers={(DOMAIN, self._device_id)}, identifiers={(DOMAIN, self._device_id)},

View File

@ -5,6 +5,8 @@ import logging
import struct import struct
from typing import Any from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_HS_COLOR, ATTR_HS_COLOR,
@ -45,7 +47,13 @@ class XiaomiGatewayLight(XiaomiDevice, LightEntity):
_attr_color_mode = ColorMode.HS _attr_color_mode = ColorMode.HS
_attr_supported_color_modes = {ColorMode.HS} _attr_supported_color_modes = {ColorMode.HS}
def __init__(self, device, name, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiGatewayLight.""" """Initialize the XiaomiGatewayLight."""
self._data_key = "rgb" self._data_key = "rgb"
self._hs = (0, 0) self._hs = (0, 0)

View File

@ -2,6 +2,10 @@
from __future__ import annotations from __future__ import annotations
from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.lock import LockEntity from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -38,7 +42,13 @@ async def async_setup_entry(
class XiaomiAqaraLock(LockEntity, XiaomiDevice): class XiaomiAqaraLock(LockEntity, XiaomiDevice):
"""Representation of a XiaomiAqaraLock.""" """Representation of a XiaomiAqaraLock."""
def __init__(self, device, name, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiAqaraLock.""" """Initialize the XiaomiAqaraLock."""
self._attr_changed_by = "0" self._attr_changed_by = "0"
self._verified_wrong_times = 0 self._verified_wrong_times = 0

View File

@ -3,6 +3,9 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -164,7 +167,14 @@ async def async_setup_entry(
class XiaomiSensor(XiaomiDevice, SensorEntity): class XiaomiSensor(XiaomiDevice, SensorEntity):
"""Representation of a XiaomiSensor.""" """Representation of a XiaomiSensor."""
def __init__(self, device, name, data_key, xiaomi_hub, config_entry): def __init__(
self,
device: dict[str, Any],
name: str,
data_key: str,
xiaomi_hub: XiaomiGateway,
config_entry: ConfigEntry,
) -> None:
"""Initialize the XiaomiSensor.""" """Initialize the XiaomiSensor."""
self._data_key = data_key self._data_key = data_key
self.entity_description = SENSOR_TYPES[data_key] self.entity_description = SENSOR_TYPES[data_key]

View File

@ -3,6 +3,8 @@
import logging import logging
from typing import Any from typing import Any
from xiaomi_gateway import XiaomiGateway
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -138,13 +140,13 @@ class XiaomiGenericSwitch(XiaomiDevice, SwitchEntity):
def __init__( def __init__(
self, self,
device, device: dict[str, Any],
name, name: str,
data_key, data_key: str,
supports_power_consumption, supports_power_consumption: bool,
xiaomi_hub, xiaomi_hub: XiaomiGateway,
config_entry, config_entry: ConfigEntry,
): ) -> None:
"""Initialize the XiaomiPlug.""" """Initialize the XiaomiPlug."""
self._data_key = data_key self._data_key = data_key
self._in_use = None self._in_use = None