From b22cd2deaa0103cd202aed969c764965f0018d7f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:40:49 +0100 Subject: [PATCH] Enable strict typing for system_health (#107283) --- .strict-typing | 1 + .../components/system_health/__init__.py | 21 ++++++++++++++----- mypy.ini | 10 +++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.strict-typing b/.strict-typing index 4a1876bbadf..f90add81fad 100644 --- a/.strict-typing +++ b/.strict-typing @@ -380,6 +380,7 @@ homeassistant.components.switchbee.* homeassistant.components.switchbot_cloud.* homeassistant.components.switcher_kis.* homeassistant.components.synology_dsm.* +homeassistant.components.system_health.* homeassistant.components.systemmonitor.* homeassistant.components.tag.* homeassistant.components.tailscale.* diff --git a/homeassistant/components/system_health/__init__.py b/homeassistant/components/system_health/__init__.py index 32970bc4fe5..cd3cad8024e 100644 --- a/homeassistant/components/system_health/__init__.py +++ b/homeassistant/components/system_health/__init__.py @@ -6,7 +6,7 @@ from collections.abc import Awaitable, Callable import dataclasses from datetime import datetime import logging -from typing import Any +from typing import Any, Protocol import aiohttp import voluptuous as vol @@ -30,13 +30,22 @@ INFO_CALLBACK_TIMEOUT = 5 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) +class SystemHealthProtocol(Protocol): + """Define the format of system_health platforms.""" + + def async_register( + self, hass: HomeAssistant, register: SystemHealthRegistration + ) -> None: + """Register system health callbacks.""" + + @bind_hass @callback def async_register_info( hass: HomeAssistant, domain: str, info_callback: Callable[[HomeAssistant], Awaitable[dict]], -): +) -> None: """Register an info callback. Deprecated. @@ -61,7 +70,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -async def _register_system_health_platform(hass, integration_domain, platform): +async def _register_system_health_platform( + hass: HomeAssistant, integration_domain: str, platform: SystemHealthProtocol +) -> None: """Register a system health platform.""" platform.async_register(hass, SystemHealthRegistration(hass, integration_domain)) @@ -89,7 +100,7 @@ async def get_integration_info( @callback -def _format_value(val): +def _format_value(val: Any) -> Any: """Format a system health value.""" if isinstance(val, datetime): return {"value": val.isoformat(), "type": "date"} @@ -207,7 +218,7 @@ class SystemHealthRegistration: self, info_callback: Callable[[HomeAssistant], Awaitable[dict]], manage_url: str | None = None, - ): + ) -> None: """Register an info callback.""" self.info_callback = info_callback self.manage_url = manage_url diff --git a/mypy.ini b/mypy.ini index d0bebf791c3..a24cad9ee7d 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3562,6 +3562,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.system_health.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.systemmonitor.*] check_untyped_defs = true disallow_incomplete_defs = true