From 1cad55f721e10e4d45633a04ec079fb1444548c8 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 27 Dec 2022 21:23:22 +0100 Subject: [PATCH] Improve `kostal_plenticore` generic typing (#84634) --- .../components/kostal_plenticore/helper.py | 34 ++++++++++++------- .../components/kostal_plenticore/number.py | 6 ++-- .../components/kostal_plenticore/select.py | 4 ++- .../components/kostal_plenticore/sensor.py | 4 ++- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/kostal_plenticore/helper.py b/homeassistant/components/kostal_plenticore/helper.py index ee684b68974..52ff3a7480b 100644 --- a/homeassistant/components/kostal_plenticore/helper.py +++ b/homeassistant/components/kostal_plenticore/helper.py @@ -6,7 +6,7 @@ from collections import defaultdict from collections.abc import Callable from datetime import datetime, timedelta import logging -from typing import Any +from typing import Any, TypeVar, cast from aiohttp.client_exceptions import ClientError from kostal.plenticore import ( @@ -26,6 +26,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN _LOGGER = logging.getLogger(__name__) +_DataT = TypeVar("_DataT") class Plenticore: @@ -154,7 +155,7 @@ class DataUpdateCoordinatorMixin: return True -class PlenticoreUpdateCoordinator(DataUpdateCoordinator): +class PlenticoreUpdateCoordinator(DataUpdateCoordinator[_DataT]): """Base implementation of DataUpdateCoordinator for Plenticore data.""" def __init__( @@ -192,7 +193,9 @@ class PlenticoreUpdateCoordinator(DataUpdateCoordinator): self._fetch[module_id].remove(data_id) -class ProcessDataUpdateCoordinator(PlenticoreUpdateCoordinator): +class ProcessDataUpdateCoordinator( + PlenticoreUpdateCoordinator[dict[str, dict[str, str]]] +): """Implementation of PlenticoreUpdateCoordinator for process data.""" async def _async_update_data(self) -> dict[str, dict[str, str]]: @@ -214,7 +217,7 @@ class ProcessDataUpdateCoordinator(PlenticoreUpdateCoordinator): class SettingDataUpdateCoordinator( - PlenticoreUpdateCoordinator, DataUpdateCoordinatorMixin + PlenticoreUpdateCoordinator[dict[str, dict[str, str]]], DataUpdateCoordinatorMixin ): """Implementation of PlenticoreUpdateCoordinator for settings data.""" @@ -230,7 +233,7 @@ class SettingDataUpdateCoordinator( return fetched_data -class PlenticoreSelectUpdateCoordinator(DataUpdateCoordinator): +class PlenticoreSelectUpdateCoordinator(DataUpdateCoordinator[_DataT]): """Base implementation of DataUpdateCoordinator for Plenticore data.""" def __init__( @@ -249,10 +252,12 @@ class PlenticoreSelectUpdateCoordinator(DataUpdateCoordinator): update_interval=update_inverval, ) # data ids to poll - self._fetch: dict[str, list[str]] = defaultdict(list) + self._fetch: dict[str, list[str | list[str]]] = defaultdict(list) self._plenticore = plenticore - def start_fetch_data(self, module_id: str, data_id: str, all_options: str) -> None: + def start_fetch_data( + self, module_id: str, data_id: str, all_options: list[str] + ) -> None: """Start fetching the given data (module-id and entry-id).""" self._fetch[module_id].append(data_id) self._fetch[module_id].append(all_options) @@ -264,14 +269,17 @@ class PlenticoreSelectUpdateCoordinator(DataUpdateCoordinator): async_call_later(self.hass, 2, force_refresh) - def stop_fetch_data(self, module_id: str, data_id: str, all_options: str) -> None: + def stop_fetch_data( + self, module_id: str, data_id: str, all_options: list[str] + ) -> None: """Stop fetching the given data (module-id and entry-id).""" self._fetch[module_id].remove(all_options) self._fetch[module_id].remove(data_id) class SelectDataUpdateCoordinator( - PlenticoreSelectUpdateCoordinator, DataUpdateCoordinatorMixin + PlenticoreSelectUpdateCoordinator[dict[str, dict[str, str]]], + DataUpdateCoordinatorMixin, ): """Implementation of PlenticoreUpdateCoordinator for select data.""" @@ -287,11 +295,11 @@ class SelectDataUpdateCoordinator( async def _async_get_current_option( self, - module_id: dict[str, list[str]], + module_id: dict[str, list[str | list[str]]], ) -> dict[str, dict[str, str]]: """Get current option.""" for mid, pids in module_id.items(): - all_options = pids[1] + all_options = cast(list[str], pids[1]) for all_option in all_options: if all_option == "None" or not ( val := await self.async_read_data(mid, all_option) @@ -299,10 +307,10 @@ class SelectDataUpdateCoordinator( continue for option in val.values(): if option[all_option] == "1": - fetched = {mid: {pids[0]: all_option}} + fetched = {mid: {cast(str, pids[0]): all_option}} return fetched - return {mid: {pids[0]: "None"}} + return {mid: {cast(str, pids[0]): "None"}} return {} diff --git a/homeassistant/components/kostal_plenticore/number.py b/homeassistant/components/kostal_plenticore/number.py index 464d528e4d2..2b0726e6255 100644 --- a/homeassistant/components/kostal_plenticore/number.py +++ b/homeassistant/components/kostal_plenticore/number.py @@ -1,7 +1,6 @@ """Platform for Kostal Plenticore numbers.""" from __future__ import annotations -from abc import ABC from dataclasses import dataclass from datetime import timedelta import logging @@ -130,11 +129,12 @@ async def async_setup_entry( async_add_entities(entities) -class PlenticoreDataNumber(CoordinatorEntity, NumberEntity, ABC): +class PlenticoreDataNumber( + CoordinatorEntity[SettingDataUpdateCoordinator], NumberEntity +): """Representation of a Kostal Plenticore Number entity.""" entity_description: PlenticoreNumberEntityDescription - coordinator: SettingDataUpdateCoordinator def __init__( self, diff --git a/homeassistant/components/kostal_plenticore/select.py b/homeassistant/components/kostal_plenticore/select.py index 307cd2d68c8..cc259bcaf35 100644 --- a/homeassistant/components/kostal_plenticore/select.py +++ b/homeassistant/components/kostal_plenticore/select.py @@ -87,7 +87,9 @@ async def async_setup_entry( async_add_entities(entities) -class PlenticoreDataSelect(CoordinatorEntity, SelectEntity): +class PlenticoreDataSelect( + CoordinatorEntity[SelectDataUpdateCoordinator], SelectEntity +): """Representation of a Plenticore Select.""" _attr_entity_category = EntityCategory.CONFIG diff --git a/homeassistant/components/kostal_plenticore/sensor.py b/homeassistant/components/kostal_plenticore/sensor.py index ff16b2c5427..f919d15d6b3 100644 --- a/homeassistant/components/kostal_plenticore/sensor.py +++ b/homeassistant/components/kostal_plenticore/sensor.py @@ -725,7 +725,9 @@ async def async_setup_entry( async_add_entities(entities) -class PlenticoreDataSensor(CoordinatorEntity, SensorEntity): +class PlenticoreDataSensor( + CoordinatorEntity[ProcessDataUpdateCoordinator], SensorEntity +): """Representation of a Plenticore data Sensor.""" entity_description: PlenticoreSensorEntityDescription