Enable strict typing for cert_expiry (#107860)

This commit is contained in:
Marc Mueller 2024-01-12 12:32:17 +01:00 committed by GitHub
parent 2508b55b0f
commit 7023ac7366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 12 deletions

View File

@ -114,6 +114,7 @@ homeassistant.components.button.*
homeassistant.components.calendar.* homeassistant.components.calendar.*
homeassistant.components.camera.* homeassistant.components.camera.*
homeassistant.components.canary.* homeassistant.components.canary.*
homeassistant.components.cert_expiry.*
homeassistant.components.clickatell.* homeassistant.components.clickatell.*
homeassistant.components.clicksend.* homeassistant.components.clicksend.*
homeassistant.components.climate.* homeassistant.components.climate.*

View File

@ -14,8 +14,8 @@ PLATFORMS = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Load the saved entities.""" """Load the saved entities."""
host = entry.data[CONF_HOST] host: str = entry.data[CONF_HOST]
port = entry.data[CONF_PORT] port: int = entry.data[CONF_PORT]
coordinator = CertExpiryDataUpdateCoordinator(hass, host, port) coordinator = CertExpiryDataUpdateCoordinator(hass, host, port)
@ -25,7 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if entry.unique_id is None: if entry.unique_id is None:
hass.config_entries.async_update_entry(entry, unique_id=f"{host}:{port}") hass.config_entries.async_update_entry(entry, unique_id=f"{host}:{port}")
async def _async_finish_startup(_): async def _async_finish_startup(_: HomeAssistant) -> None:
await coordinator.async_refresh() await coordinator.async_refresh()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

View File

@ -35,7 +35,7 @@ class CertexpiryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _test_connection( async def _test_connection(
self, self,
user_input: Mapping[str, Any], user_input: Mapping[str, Any],
): ) -> bool:
"""Test connection to the server and try to get the certificate.""" """Test connection to the server and try to get the certificate."""
try: try:
await get_cert_expiry_timestamp( await get_cert_expiry_timestamp(

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DEFAULT_PORT from .const import DEFAULT_PORT
@ -16,11 +17,11 @@ _LOGGER = logging.getLogger(__name__)
class CertExpiryDataUpdateCoordinator(DataUpdateCoordinator[datetime | None]): class CertExpiryDataUpdateCoordinator(DataUpdateCoordinator[datetime | None]):
"""Class to manage fetching Cert Expiry data from single endpoint.""" """Class to manage fetching Cert Expiry data from single endpoint."""
def __init__(self, hass, host, port): def __init__(self, hass: HomeAssistant, host: str, port: int) -> None:
"""Initialize global Cert Expiry data updater.""" """Initialize global Cert Expiry data updater."""
self.host = host self.host = host
self.port = port self.port = port
self.cert_error = None self.cert_error: ValidationFailure | None = None
self.is_cert_valid = False self.is_cert_valid = False
display_port = f":{port}" if port != DEFAULT_PORT else "" display_port = f":{port}" if port != DEFAULT_PORT else ""

View File

@ -19,7 +19,7 @@ from .errors import (
@cache @cache
def _get_default_ssl_context(): def _get_default_ssl_context() -> ssl.SSLContext:
"""Return the default SSL context.""" """Return the default SSL context."""
return ssl.create_default_context() return ssl.create_default_context()
@ -40,7 +40,7 @@ async def async_get_cert(
server_hostname=host, server_hostname=host,
) )
try: try:
return transport.get_extra_info("peercert") return transport.get_extra_info("peercert") # type: ignore[no-any-return]
finally: finally:
transport.close() transport.close()

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -12,7 +13,7 @@ from homeassistant.components.sensor import (
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_START from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_START
from homeassistant.core import HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -42,12 +43,12 @@ async def async_setup_platform(
"""Set up certificate expiry sensor.""" """Set up certificate expiry sensor."""
@callback @callback
def schedule_import(_): def schedule_import(_: Event) -> None:
"""Schedule delayed import after HA is fully started.""" """Schedule delayed import after HA is fully started."""
async_call_later(hass, 10, do_import) async_call_later(hass, 10, do_import)
@callback @callback
def do_import(_): def do_import(_: datetime) -> None:
"""Process YAML import.""" """Process YAML import."""
hass.async_create_task( hass.async_create_task(
hass.config_entries.flow.async_init( hass.config_entries.flow.async_init(
@ -80,7 +81,7 @@ class CertExpiryEntity(CoordinatorEntity[CertExpiryDataUpdateCoordinator]):
_attr_has_entity_name = True _attr_has_entity_name = True
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any]:
"""Return additional sensor state attributes.""" """Return additional sensor state attributes."""
return { return {
"is_valid": self.coordinator.is_cert_valid, "is_valid": self.coordinator.is_cert_valid,

View File

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