Skip to content

Commit

Permalink
Add typings to Certificate Expiry integration (home-assistant#75945)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 authored Jul 31, 2022
1 parent abb7495 commit 1204b4f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
18 changes: 15 additions & 3 deletions homeassistant/components/cert_expiry/config_flow.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions homeassistant/components/cert_expiry/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import socket
import ssl

from homeassistant.core import HomeAssistant
from homeassistant.util import dt

from .const import TIMEOUT
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions homeassistant/components/cert_expiry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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})"
Expand Down

0 comments on commit 1204b4f

Please sign in to comment.