Skip to content

Commit

Permalink
Add type annotations and shorten sensor names on ezviz sensor platfor…
Browse files Browse the repository at this point in the history
…ms (home-assistant#52475)

* Add basic typing and change name on sensor platforms.

* Complete type annotations for all functions.
  • Loading branch information
RenierM26 authored Jul 5, 2021
1 parent 64e63de commit 2e4f513
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
35 changes: 26 additions & 9 deletions homeassistant/components/ezviz/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
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):
Expand All @@ -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
Expand All @@ -45,22 +62,22 @@ def __init__(self, coordinator, idx, name, sensor_type_name):
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)},
Expand All @@ -71,6 +88,6 @@ def device_info(self):
}

@property
def device_class(self):
def device_class(self) -> str:
"""Device class for the sensor."""
return self.sensor_type_name
38 changes: 28 additions & 10 deletions homeassistant/components/ezviz/sensor.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -43,22 +61,22 @@ def __init__(self, coordinator, idx, name, sensor_type_name):
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)},
Expand All @@ -69,6 +87,6 @@ def device_info(self):
}

@property
def device_class(self):
def device_class(self) -> str:
"""Device class for the sensor."""
return self.sensor_type_name

0 comments on commit 2e4f513

Please sign in to comment.