Enable strict typing for nightscout (#107307)

This commit is contained in:
Marc Mueller 2024-01-08 10:43:40 +01:00 committed by GitHub
parent b22cd2deaa
commit c30bf1f6e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 7 deletions

View File

@ -289,6 +289,7 @@ homeassistant.components.netatmo.*
homeassistant.components.network.* homeassistant.components.network.*
homeassistant.components.nextdns.* homeassistant.components.nextdns.*
homeassistant.components.nfandroidtv.* homeassistant.components.nfandroidtv.*
homeassistant.components.nightscout.*
homeassistant.components.nissan_leaf.* homeassistant.components.nissan_leaf.*
homeassistant.components.no_ip.* homeassistant.components.no_ip.*
homeassistant.components.notify.* homeassistant.components.notify.*

View File

@ -1,6 +1,7 @@
"""Config flow for Nightscout integration.""" """Config flow for Nightscout integration."""
from asyncio import TimeoutError as AsyncIOTimeoutError from asyncio import TimeoutError as AsyncIOTimeoutError
import logging import logging
from typing import Any
from aiohttp import ClientError, ClientResponseError from aiohttp import ClientError, ClientResponseError
from py_nightscout import Api as NightscoutAPI from py_nightscout import Api as NightscoutAPI
@ -8,6 +9,7 @@ import voluptuous as vol
from homeassistant import config_entries, exceptions from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_API_KEY, CONF_URL from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN from .const import DOMAIN
from .utils import hash_from_url 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}) 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.""" """Validate the user input allows us to connect."""
url = data[CONF_URL] url: str = data[CONF_URL]
api_key = data.get(CONF_API_KEY) api_key: str | None = data.get(CONF_API_KEY)
try: try:
api = NightscoutAPI(url, api_secret=api_key) api = NightscoutAPI(url, api_secret=api_key)
status = await api.get_server_status() status = await api.get_server_status()
@ -40,9 +42,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1 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.""" """Handle the initial step."""
errors = {} errors: dict[str, str] = {}
if user_input is not None: if user_input is not None:
unique_id = hash_from_url(user_input[CONF_URL]) unique_id = hash_from_url(user_input[CONF_URL])

View File

@ -40,7 +40,7 @@ class NightscoutSensor(SensorEntity):
_attr_native_unit_of_measurement = "mg/dL" _attr_native_unit_of_measurement = "mg/dL"
_attr_icon = "mdi:cloud-question" _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.""" """Initialize the Nightscout sensor."""
self.api = api self.api = api
self._attr_unique_id = unique_id self._attr_unique_id = unique_id

View File

@ -1,7 +1,9 @@
"""Nightscout util functions.""" """Nightscout util functions."""
from __future__ import annotations
import hashlib import hashlib
def hash_from_url(url: str): def hash_from_url(url: str) -> str:
"""Hash url to create a unique ID.""" """Hash url to create a unique ID."""
return hashlib.sha256(url.encode("utf-8")).hexdigest() return hashlib.sha256(url.encode("utf-8")).hexdigest()

View File

@ -2651,6 +2651,16 @@ disallow_untyped_defs = true
warn_return_any = true warn_return_any = true
warn_unreachable = 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.*] [mypy-homeassistant.components.nissan_leaf.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true