From 1204b4f7000cda093290ecf3d07eaca2d00bdee1 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Sun, 31 Jul 2022 12:26:16 +0100 Subject: [PATCH] Add typings to Certificate Expiry integration (#75945) --- .../components/cert_expiry/config_flow.py | 18 +++++++++++++++--- homeassistant/components/cert_expiry/helper.py | 12 ++++++++++-- homeassistant/components/cert_expiry/sensor.py | 10 ++++++++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/cert_expiry/config_flow.py b/homeassistant/components/cert_expiry/config_flow.py index 13336c597715a..ed294cab981b6 100644 --- a/homeassistant/components/cert_expiry/config_flow.py +++ b/homeassistant/components/cert_expiry/config_flow.py @@ -1,12 +1,15 @@ """Config flow for the Cert Expiry platform.""" from __future__ import annotations +from collections.abc import Mapping import logging +from typing import Any import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_HOST, CONF_PORT +from homeassistant.data_entry_flow import FlowResult from .const import DEFAULT_PORT, DOMAIN from .errors import ( @@ -29,7 +32,10 @@ def __init__(self) -> None: """Initialize the config flow.""" self._errors: dict[str, str] = {} - async def _test_connection(self, user_input=None): + async def _test_connection( + self, + user_input: Mapping[str, Any], + ): """Test connection to the server and try to get the certificate.""" try: await get_cert_expiry_timestamp( @@ -48,7 +54,10 @@ async def _test_connection(self, user_input=None): return True return False - async def async_step_user(self, user_input=None): + async def async_step_user( + self, + user_input: Mapping[str, Any] | None = None, + ) -> FlowResult: """Step when user initializes a integration.""" self._errors = {} if user_input is not None: @@ -85,7 +94,10 @@ async def async_step_user(self, user_input=None): errors=self._errors, ) - async def async_step_import(self, user_input=None): + async def async_step_import( + self, + user_input: Mapping[str, Any] | None = None, + ) -> FlowResult: """Import a config entry. Only host was required in the yaml file all other fields are optional diff --git a/homeassistant/components/cert_expiry/helper.py b/homeassistant/components/cert_expiry/helper.py index c00a99c8e86da..fcd808a6521f5 100644 --- a/homeassistant/components/cert_expiry/helper.py +++ b/homeassistant/components/cert_expiry/helper.py @@ -2,6 +2,7 @@ import socket import ssl +from homeassistant.core import HomeAssistant from homeassistant.util import dt from .const import TIMEOUT @@ -13,7 +14,10 @@ ) -def get_cert(host, port): +def get_cert( + host: str, + port: int, +): """Get the certificate for the host and port combination.""" ctx = ssl.create_default_context() address = (host, port) @@ -23,7 +27,11 @@ def get_cert(host, port): return cert -async def get_cert_expiry_timestamp(hass, hostname, port): +async def get_cert_expiry_timestamp( + hass: HomeAssistant, + hostname: str, + port: int, +): """Return the certificate's expiration timestamp.""" try: cert = await hass.async_add_executor_job(get_cert, hostname, port) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index 9ee79e5c4499c..0c1b6116cddc0 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -19,6 +19,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.update_coordinator import CoordinatorEntity +from . import CertExpiryDataUpdateCoordinator from .const import DEFAULT_PORT, DOMAIN SCAN_INTERVAL = timedelta(hours=12) @@ -57,7 +58,9 @@ def do_import(_): async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Add cert-expiry entry.""" coordinator = hass.data[DOMAIN][entry.entry_id] @@ -88,7 +91,10 @@ class SSLCertificateTimestamp(CertExpiryEntity, SensorEntity): _attr_device_class = SensorDeviceClass.TIMESTAMP - def __init__(self, coordinator) -> None: + def __init__( + self, + coordinator: CertExpiryDataUpdateCoordinator, + ) -> None: """Initialize a Cert Expiry timestamp sensor.""" super().__init__(coordinator) self._attr_name = f"Cert Expiry Timestamp ({coordinator.name})"