Surepetcare, strict typing (#56425)

* Surepetcare, strict typing

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Surepetcare, strict typing

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
Daniel Hjelseth Høyer 2021-09-19 20:57:28 +02:00 committed by GitHub
parent d76163e5be
commit 00f7548fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 11 deletions

View File

@ -98,6 +98,7 @@ homeassistant.components.sonos.media_player
homeassistant.components.ssdp.* homeassistant.components.ssdp.*
homeassistant.components.stream.* homeassistant.components.stream.*
homeassistant.components.sun.* homeassistant.components.sun.*
homeassistant.components.surepetcare.*
homeassistant.components.switch.* homeassistant.components.switch.*
homeassistant.components.switcher_kis.* homeassistant.components.switcher_kis.*
homeassistant.components.synology_dsm.* homeassistant.components.synology_dsm.*

View File

@ -3,8 +3,10 @@ from __future__ import annotations
from abc import abstractmethod from abc import abstractmethod
import logging import logging
from typing import cast
from surepy.entities import SurepyEntity from surepy.entities import SurepyEntity
from surepy.entities.pet import Pet as SurepyPet
from surepy.enums import EntityType, Location from surepy.enums import EntityType, Location
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -12,7 +14,9 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_PRESENCE, DEVICE_CLASS_PRESENCE,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.core import callback from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
@ -23,10 +27,12 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities) -> None: async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Sure PetCare Flaps binary sensors based on a config entry.""" """Set up Sure PetCare Flaps binary sensors based on a config entry."""
entities: list[SurepyEntity | Pet | Hub | DeviceConnectivity] = [] entities: list[SurePetcareBinarySensor] = []
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
@ -75,7 +81,7 @@ class SurePetcareBinarySensor(CoordinatorEntity, BinarySensorEntity):
@abstractmethod @abstractmethod
@callback @callback
def _update_attr(self, surepy_entity) -> None: def _update_attr(self, surepy_entity: SurepyEntity) -> None:
"""Update the state and attributes.""" """Update the state and attributes."""
@callback @callback
@ -96,7 +102,7 @@ class Hub(SurePetcareBinarySensor):
return super().available and bool(self._attr_is_on) return super().available and bool(self._attr_is_on)
@callback @callback
def _update_attr(self, surepy_entity) -> None: def _update_attr(self, surepy_entity: SurepyEntity) -> None:
"""Get the latest data and update the state.""" """Get the latest data and update the state."""
state = surepy_entity.raw_data()["status"] state = surepy_entity.raw_data()["status"]
self._attr_is_on = self._attr_available = bool(state["online"]) self._attr_is_on = self._attr_available = bool(state["online"])
@ -118,8 +124,9 @@ class Pet(SurePetcareBinarySensor):
_attr_device_class = DEVICE_CLASS_PRESENCE _attr_device_class = DEVICE_CLASS_PRESENCE
@callback @callback
def _update_attr(self, surepy_entity) -> None: def _update_attr(self, surepy_entity: SurepyEntity) -> None:
"""Get the latest data and update the state.""" """Get the latest data and update the state."""
surepy_entity = cast(SurepyPet, surepy_entity)
state = surepy_entity.location state = surepy_entity.location
try: try:
self._attr_is_on = bool(Location(state.where) == Location.INSIDE) self._attr_is_on = bool(Location(state.where) == Location.INSIDE)
@ -153,7 +160,7 @@ class DeviceConnectivity(SurePetcareBinarySensor):
) )
@callback @callback
def _update_attr(self, surepy_entity): def _update_attr(self, surepy_entity: SurepyEntity) -> None:
state = surepy_entity.raw_data()["status"] state = surepy_entity.raw_data()["status"]
self._attr_is_on = bool(state) self._attr_is_on = bool(state)
if state: if state:

View File

@ -46,7 +46,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
async def async_step_import(self, import_info): async def async_step_import(self, import_info: dict[str, Any] | None) -> FlowResult:
"""Set the config entry up from yaml.""" """Set the config entry up from yaml."""
return await self.async_step_user(import_info) return await self.async_step_user(import_info)

View File

@ -7,8 +7,10 @@ from surepy.entities import SurepyEntity
from surepy.enums import EntityType from surepy.enums import EntityType
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_VOLTAGE, DEVICE_CLASS_BATTERY, PERCENTAGE from homeassistant.const import ATTR_VOLTAGE, DEVICE_CLASS_BATTERY, PERCENTAGE
from homeassistant.core import callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
@ -19,10 +21,12 @@ from .const import DOMAIN, SURE_BATT_VOLTAGE_DIFF, SURE_BATT_VOLTAGE_LOW
_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 Sure PetCare Flaps sensors.""" """Set up Sure PetCare Flaps sensors."""
entities: list[SurepyEntity] = [] entities: list[SureBattery] = []
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]

View File

@ -1089,6 +1089,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.surepetcare.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.switch.*] [mypy-homeassistant.components.switch.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true