Add type annotations and shorten sensor names on ezviz sensor platforms (#52475)
* Add basic typing and change name on sensor platforms. * Complete type annotations for all functions.
This commit is contained in:
parent
64e63dedf6
commit
2e4f513526
2 changed files with 54 additions and 19 deletions
|
@ -4,16 +4,25 @@ import logging
|
||||||
from pyezviz.constants import BinarySensorType
|
from pyezviz.constants import BinarySensorType
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
||||||
|
from .coordinator import EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
"""Set up Ezviz sensors based on a config entry."""
|
"""Set up Ezviz sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||||
|
DATA_COORDINATOR
|
||||||
|
]
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
for idx, camera in enumerate(coordinator.data):
|
for idx, camera in enumerate(coordinator.data):
|
||||||
|
@ -34,7 +43,15 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||||
"""Representation of a Ezviz sensor."""
|
"""Representation of a Ezviz sensor."""
|
||||||
|
|
||||||
def __init__(self, coordinator, idx, name, sensor_type_name):
|
coordinator: EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: EzvizDataUpdateCoordinator,
|
||||||
|
idx: int,
|
||||||
|
name: str,
|
||||||
|
sensor_type_name: str,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._idx = idx
|
self._idx = idx
|
||||||
|
@ -45,22 +62,22 @@ class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||||
self._serial = self.coordinator.data[self._idx]["serial"]
|
self._serial = self.coordinator.data[self._idx]["serial"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the Ezviz sensor."""
|
"""Return the name of the Ezviz sensor."""
|
||||||
return self._sensor_name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data[self._idx][self._name]
|
return self.coordinator.data[self._idx][self._name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique ID of this sensor."""
|
"""Return the unique ID of this sensor."""
|
||||||
return f"{self._serial}_{self._sensor_name}"
|
return f"{self._serial}_{self._sensor_name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(DOMAIN, self._serial)},
|
"identifiers": {(DOMAIN, self._serial)},
|
||||||
|
@ -71,6 +88,6 @@ class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self) -> str:
|
||||||
"""Device class for the sensor."""
|
"""Device class for the sensor."""
|
||||||
return self.sensor_type_name
|
return self.sensor_type_name
|
||||||
|
|
|
@ -1,19 +1,29 @@
|
||||||
"""Support for Ezviz sensors."""
|
"""Support for Ezviz sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pyezviz.constants import SensorType
|
from pyezviz.constants import SensorType
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
||||||
|
from .coordinator import EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
"""Set up Ezviz sensors based on a config entry."""
|
"""Set up Ezviz sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||||
|
DATA_COORDINATOR
|
||||||
|
]
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
for idx, camera in enumerate(coordinator.data):
|
for idx, camera in enumerate(coordinator.data):
|
||||||
|
@ -32,7 +42,15 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
class EzvizSensor(CoordinatorEntity, Entity):
|
class EzvizSensor(CoordinatorEntity, Entity):
|
||||||
"""Representation of a Ezviz sensor."""
|
"""Representation of a Ezviz sensor."""
|
||||||
|
|
||||||
def __init__(self, coordinator, idx, name, sensor_type_name):
|
coordinator: EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: EzvizDataUpdateCoordinator,
|
||||||
|
idx: int,
|
||||||
|
name: str,
|
||||||
|
sensor_type_name: str,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._idx = idx
|
self._idx = idx
|
||||||
|
@ -43,22 +61,22 @@ class EzvizSensor(CoordinatorEntity, Entity):
|
||||||
self._serial = self.coordinator.data[self._idx]["serial"]
|
self._serial = self.coordinator.data[self._idx]["serial"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the Ezviz sensor."""
|
"""Return the name of the Ezviz sensor."""
|
||||||
return self._sensor_name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> int | str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data[self._idx][self._name]
|
return self.coordinator.data[self._idx][self._name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique ID of this sensor."""
|
"""Return the unique ID of this sensor."""
|
||||||
return f"{self._serial}_{self._sensor_name}"
|
return f"{self._serial}_{self._sensor_name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(DOMAIN, self._serial)},
|
"identifiers": {(DOMAIN, self._serial)},
|
||||||
|
@ -69,6 +87,6 @@ class EzvizSensor(CoordinatorEntity, Entity):
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self) -> str:
|
||||||
"""Device class for the sensor."""
|
"""Device class for the sensor."""
|
||||||
return self.sensor_type_name
|
return self.sensor_type_name
|
||||||
|
|
Loading…
Add table
Reference in a new issue