Add type annotations to amcrest integration (#54761)

Co-authored-by: Milan Meulemans <milan.meulemans@live.be>
This commit is contained in:
Sean Vig 2021-08-25 07:24:29 -04:00 committed by GitHub
parent bb42eb1176
commit 6b4e3bca6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 282 additions and 165 deletions

View file

@ -5,6 +5,7 @@ from contextlib import suppress
from dataclasses import dataclass
from datetime import timedelta
import logging
from typing import TYPE_CHECKING, Callable
from amcrest import AmcrestError
import voluptuous as vol
@ -17,8 +18,10 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.const import CONF_BINARY_SENSORS, CONF_NAME
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import Throttle
from .const import (
@ -30,6 +33,9 @@ from .const import (
)
from .helpers import log_update_error, service_signal
if TYPE_CHECKING:
from . import AmcrestDevice
@dataclass
class AmcrestSensorEntityDescription(BinarySensorEntityDescription):
@ -117,7 +123,7 @@ _EXCLUSIVE_OPTIONS = [
_UPDATE_MSG = "Updating %s binary sensor"
def check_binary_sensors(value):
def check_binary_sensors(value: list[str]) -> list[str]:
"""Validate binary sensor configurations."""
for exclusive_options in _EXCLUSIVE_OPTIONS:
if len(set(value) & exclusive_options) > 1:
@ -127,7 +133,12 @@ def check_binary_sensors(value):
return value
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up a binary sensor for an Amcrest IP Camera."""
if discovery_info is None:
return
@ -148,21 +159,27 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class AmcrestBinarySensor(BinarySensorEntity):
"""Binary sensor for Amcrest camera."""
def __init__(self, name, device, entity_description):
def __init__(
self,
name: str,
device: AmcrestDevice,
entity_description: AmcrestSensorEntityDescription,
) -> None:
"""Initialize entity."""
self._signal_name = name
self._api = device.api
self.entity_description = entity_description
self.entity_description: AmcrestSensorEntityDescription = entity_description
self._attr_name = f"{name} {entity_description.name}"
self._attr_should_poll = entity_description.should_poll
self._unsub_dispatcher = []
self._unsub_dispatcher: list[Callable[[], None]] = []
@property
def available(self):
def available(self) -> bool:
"""Return True if entity is available."""
return self.entity_description.key == _ONLINE_KEY or self._api.available
def update(self):
def update(self) -> None:
"""Update entity."""
if self.entity_description.key == _ONLINE_KEY:
self._update_online()
@ -170,7 +187,7 @@ class AmcrestBinarySensor(BinarySensorEntity):
self._update_others()
@Throttle(_ONLINE_SCAN_INTERVAL)
def _update_online(self):
def _update_online(self) -> None:
if not (self._api.available or self.is_on):
return
_LOGGER.debug(_UPDATE_MSG, self.name)
@ -182,37 +199,41 @@ class AmcrestBinarySensor(BinarySensorEntity):
self._api.current_time # pylint: disable=pointless-statement
self._attr_is_on = self._api.available
def _update_others(self):
def _update_others(self) -> None:
if not self.available:
return
_LOGGER.debug(_UPDATE_MSG, self.name)
event_code = self.entity_description.event_code
if event_code is None:
_LOGGER.error("Binary sensor %s event code not set", self.name)
return
try:
self._attr_is_on = "channels" in self._api.event_channels_happened(
event_code
)
self._attr_is_on = len(self._api.event_channels_happened(event_code)) > 0
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
async def async_on_demand_update(self):
async def async_on_demand_update(self) -> None:
"""Update state."""
if self.entity_description.key == _ONLINE_KEY:
_LOGGER.debug(_UPDATE_MSG, self.name)
self._attr_is_on = self._api.available
self.async_write_ha_state()
return
self.async_schedule_update_ha_state(True)
else:
self.async_schedule_update_ha_state(True)
@callback
def async_event_received(self, start):
def async_event_received(self, state: bool) -> None:
"""Update state from received event."""
_LOGGER.debug(_UPDATE_MSG, self.name)
self._attr_is_on = start
self._attr_is_on = state
self.async_write_ha_state()
async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""Subscribe to signals."""
assert self.hass is not None
self._unsub_dispatcher.append(
async_dispatcher_connect(
self.hass,
@ -236,7 +257,7 @@ class AmcrestBinarySensor(BinarySensorEntity):
)
)
async def async_will_remove_from_hass(self):
async def async_will_remove_from_hass(self) -> None:
"""Disconnect from update signal."""
for unsub_dispatcher in self._unsub_dispatcher:
unsub_dispatcher()