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.
pull/52541/head
RenierM26 2021-07-05 11:40:13 +02:00 committed by GitHub
parent 64e63dedf6
commit 2e4f513526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 19 deletions

View File

@ -4,16 +4,25 @@ import logging
from pyezviz.constants import BinarySensorType
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 .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
from .coordinator import EzvizDataUpdateCoordinator
_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."""
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
sensors = []
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):
"""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."""
super().__init__(coordinator)
self._idx = idx
@ -45,22 +62,22 @@ class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
self._serial = self.coordinator.data[self._idx]["serial"]
@property
def name(self):
def name(self) -> str:
"""Return the name of the Ezviz sensor."""
return self._sensor_name
return self._name
@property
def is_on(self):
def is_on(self) -> bool:
"""Return the state of the sensor."""
return self.coordinator.data[self._idx][self._name]
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return the unique ID of this sensor."""
return f"{self._serial}_{self._sensor_name}"
@property
def device_info(self):
def device_info(self) -> DeviceInfo:
"""Return the device_info of the device."""
return {
"identifiers": {(DOMAIN, self._serial)},
@ -71,6 +88,6 @@ class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
}
@property
def device_class(self):
def device_class(self) -> str:
"""Device class for the sensor."""
return self.sensor_type_name

View File

@ -1,19 +1,29 @@
"""Support for Ezviz sensors."""
from __future__ import annotations
import logging
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 .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
from .coordinator import EzvizDataUpdateCoordinator
_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."""
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
sensors = []
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):
"""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."""
super().__init__(coordinator)
self._idx = idx
@ -43,22 +61,22 @@ class EzvizSensor(CoordinatorEntity, Entity):
self._serial = self.coordinator.data[self._idx]["serial"]
@property
def name(self):
def name(self) -> str:
"""Return the name of the Ezviz sensor."""
return self._sensor_name
return self._name
@property
def state(self):
def state(self) -> int | str:
"""Return the state of the sensor."""
return self.coordinator.data[self._idx][self._name]
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return the unique ID of this sensor."""
return f"{self._serial}_{self._sensor_name}"
@property
def device_info(self):
def device_info(self) -> DeviceInfo:
"""Return the device_info of the device."""
return {
"identifiers": {(DOMAIN, self._serial)},
@ -69,6 +87,6 @@ class EzvizSensor(CoordinatorEntity, Entity):
}
@property
def device_class(self):
def device_class(self) -> str:
"""Device class for the sensor."""
return self.sensor_type_name