From c30bf1f6e1ec359c735e2bd9336c30a1f1e63b29 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:43:40 +0100 Subject: [PATCH] Enable strict typing for nightscout (#107307) --- .strict-typing | 1 + homeassistant/components/nightscout/config_flow.py | 14 +++++++++----- homeassistant/components/nightscout/sensor.py | 2 +- homeassistant/components/nightscout/utils.py | 4 +++- mypy.ini | 10 ++++++++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.strict-typing b/.strict-typing index f90add81fad..648379875da 100644 --- a/.strict-typing +++ b/.strict-typing @@ -289,6 +289,7 @@ homeassistant.components.netatmo.* homeassistant.components.network.* homeassistant.components.nextdns.* homeassistant.components.nfandroidtv.* +homeassistant.components.nightscout.* homeassistant.components.nissan_leaf.* homeassistant.components.no_ip.* homeassistant.components.notify.* diff --git a/homeassistant/components/nightscout/config_flow.py b/homeassistant/components/nightscout/config_flow.py index 1f3f62835bc..98e075ba3c9 100644 --- a/homeassistant/components/nightscout/config_flow.py +++ b/homeassistant/components/nightscout/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Nightscout integration.""" from asyncio import TimeoutError as AsyncIOTimeoutError import logging +from typing import Any from aiohttp import ClientError, ClientResponseError from py_nightscout import Api as NightscoutAPI @@ -8,6 +9,7 @@ import voluptuous as vol from homeassistant import config_entries, exceptions from homeassistant.const import CONF_API_KEY, CONF_URL +from homeassistant.data_entry_flow import FlowResult from .const import DOMAIN from .utils import hash_from_url @@ -17,10 +19,10 @@ _LOGGER = logging.getLogger(__name__) DATA_SCHEMA = vol.Schema({vol.Required(CONF_URL): str, vol.Optional(CONF_API_KEY): str}) -async def _validate_input(data): +async def _validate_input(data: dict[str, Any]) -> dict[str, str]: """Validate the user input allows us to connect.""" - url = data[CONF_URL] - api_key = data.get(CONF_API_KEY) + url: str = data[CONF_URL] + api_key: str | None = data.get(CONF_API_KEY) try: api = NightscoutAPI(url, api_secret=api_key) status = await api.get_server_status() @@ -40,9 +42,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle the initial step.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: unique_id = hash_from_url(user_input[CONF_URL]) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index f60c70cc67c..851610ee374 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -40,7 +40,7 @@ class NightscoutSensor(SensorEntity): _attr_native_unit_of_measurement = "mg/dL" _attr_icon = "mdi:cloud-question" - def __init__(self, api: NightscoutAPI, name, unique_id) -> None: + def __init__(self, api: NightscoutAPI, name: str, unique_id: str | None) -> None: """Initialize the Nightscout sensor.""" self.api = api self._attr_unique_id = unique_id diff --git a/homeassistant/components/nightscout/utils.py b/homeassistant/components/nightscout/utils.py index 4d262ee6439..ac9ce1a3384 100644 --- a/homeassistant/components/nightscout/utils.py +++ b/homeassistant/components/nightscout/utils.py @@ -1,7 +1,9 @@ """Nightscout util functions.""" +from __future__ import annotations + import hashlib -def hash_from_url(url: str): +def hash_from_url(url: str) -> str: """Hash url to create a unique ID.""" return hashlib.sha256(url.encode("utf-8")).hexdigest() diff --git a/mypy.ini b/mypy.ini index a24cad9ee7d..2766b47b471 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2651,6 +2651,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.nightscout.*] +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.nissan_leaf.*] check_untyped_defs = true disallow_incomplete_defs = true