From 00f7548fa0454227199275e3fabb36d8f6e0f39c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= <mail@dahoiv.net>
Date: Sun, 19 Sep 2021 20:57:28 +0200
Subject: [PATCH] Surepetcare, strict typing (#56425)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 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>
---
 .strict-typing                                |  1 +
 .../components/surepetcare/binary_sensor.py   | 21 ++++++++++++-------
 .../components/surepetcare/config_flow.py     |  2 +-
 .../components/surepetcare/sensor.py          | 10 ++++++---
 mypy.ini                                      | 11 ++++++++++
 5 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/.strict-typing b/.strict-typing
index 64fbcb9e82d..df0c0168a9e 100644
--- a/.strict-typing
+++ b/.strict-typing
@@ -98,6 +98,7 @@ homeassistant.components.sonos.media_player
 homeassistant.components.ssdp.*
 homeassistant.components.stream.*
 homeassistant.components.sun.*
+homeassistant.components.surepetcare.*
 homeassistant.components.switch.*
 homeassistant.components.switcher_kis.*
 homeassistant.components.synology_dsm.*
diff --git a/homeassistant/components/surepetcare/binary_sensor.py b/homeassistant/components/surepetcare/binary_sensor.py
index 2f411e8c2a9..e0903230697 100644
--- a/homeassistant/components/surepetcare/binary_sensor.py
+++ b/homeassistant/components/surepetcare/binary_sensor.py
@@ -3,8 +3,10 @@ from __future__ import annotations
 
 from abc import abstractmethod
 import logging
+from typing import cast
 
 from surepy.entities import SurepyEntity
+from surepy.entities.pet import Pet as SurepyPet
 from surepy.enums import EntityType, Location
 
 from homeassistant.components.binary_sensor import (
@@ -12,7 +14,9 @@ from homeassistant.components.binary_sensor import (
     DEVICE_CLASS_PRESENCE,
     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 (
     CoordinatorEntity,
     DataUpdateCoordinator,
@@ -23,10 +27,12 @@ from .const import DOMAIN
 _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."""
 
-    entities: list[SurepyEntity | Pet | Hub | DeviceConnectivity] = []
+    entities: list[SurePetcareBinarySensor] = []
 
     coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
 
@@ -75,7 +81,7 @@ class SurePetcareBinarySensor(CoordinatorEntity, BinarySensorEntity):
 
     @abstractmethod
     @callback
-    def _update_attr(self, surepy_entity) -> None:
+    def _update_attr(self, surepy_entity: SurepyEntity) -> None:
         """Update the state and attributes."""
 
     @callback
@@ -96,7 +102,7 @@ class Hub(SurePetcareBinarySensor):
         return super().available and bool(self._attr_is_on)
 
     @callback
-    def _update_attr(self, surepy_entity) -> None:
+    def _update_attr(self, surepy_entity: SurepyEntity) -> None:
         """Get the latest data and update the state."""
         state = surepy_entity.raw_data()["status"]
         self._attr_is_on = self._attr_available = bool(state["online"])
@@ -118,8 +124,9 @@ class Pet(SurePetcareBinarySensor):
     _attr_device_class = DEVICE_CLASS_PRESENCE
 
     @callback
-    def _update_attr(self, surepy_entity) -> None:
+    def _update_attr(self, surepy_entity: SurepyEntity) -> None:
         """Get the latest data and update the state."""
+        surepy_entity = cast(SurepyPet, surepy_entity)
         state = surepy_entity.location
         try:
             self._attr_is_on = bool(Location(state.where) == Location.INSIDE)
@@ -153,7 +160,7 @@ class DeviceConnectivity(SurePetcareBinarySensor):
         )
 
     @callback
-    def _update_attr(self, surepy_entity):
+    def _update_attr(self, surepy_entity: SurepyEntity) -> None:
         state = surepy_entity.raw_data()["status"]
         self._attr_is_on = bool(state)
         if state:
diff --git a/homeassistant/components/surepetcare/config_flow.py b/homeassistant/components/surepetcare/config_flow.py
index e2e5f07f05e..bc3589aa4bb 100644
--- a/homeassistant/components/surepetcare/config_flow.py
+++ b/homeassistant/components/surepetcare/config_flow.py
@@ -46,7 +46,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
 
     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."""
         return await self.async_step_user(import_info)
 
diff --git a/homeassistant/components/surepetcare/sensor.py b/homeassistant/components/surepetcare/sensor.py
index 0122dc2905d..a52c1d7d0ed 100644
--- a/homeassistant/components/surepetcare/sensor.py
+++ b/homeassistant/components/surepetcare/sensor.py
@@ -7,8 +7,10 @@ from surepy.entities import SurepyEntity
 from surepy.enums import EntityType
 
 from homeassistant.components.sensor import SensorEntity
+from homeassistant.config_entries import ConfigEntry
 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 (
     CoordinatorEntity,
     DataUpdateCoordinator,
@@ -19,10 +21,12 @@ from .const import DOMAIN, SURE_BATT_VOLTAGE_DIFF, SURE_BATT_VOLTAGE_LOW
 _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."""
 
-    entities: list[SurepyEntity] = []
+    entities: list[SureBattery] = []
 
     coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
 
diff --git a/mypy.ini b/mypy.ini
index fc3d76863ef..b1b2657aac9 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -1089,6 +1089,17 @@ no_implicit_optional = true
 warn_return_any = 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.*]
 check_untyped_defs = true
 disallow_incomplete_defs = true
-- 
GitLab