From 943ce8afaff83a69ab05410a68e5321d58609d93 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Fri, 19 Mar 2021 10:16:27 +0100 Subject: [PATCH] Type check KNX integration weather, notify and scene (#48051) --- homeassistant/components/knx/notify.py | 23 +++++++++++----- homeassistant/components/knx/scene.py | 20 +++++++++++--- homeassistant/components/knx/weather.py | 35 ++++++++++++++++++------- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/knx/notify.py b/homeassistant/components/knx/notify.py index 9d6ed35e36b..77b89b86c4d 100644 --- a/homeassistant/components/knx/notify.py +++ b/homeassistant/components/knx/notify.py @@ -1,14 +1,25 @@ """Support for KNX/IP notification services.""" from __future__ import annotations +from typing import Any + from xknx.devices import Notification as XknxNotification from homeassistant.components.notify import BaseNotificationService +from homeassistant.helpers.typing import ( + ConfigType, + DiscoveryInfoType, + HomeAssistantType, +) from .const import DOMAIN -async def async_get_service(hass, config, discovery_info=None): +async def async_get_service( + hass: HomeAssistantType, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, +) -> KNXNotificationService | None: """Get the KNX notification service.""" notification_devices = [] for device in hass.data[DOMAIN].xknx.devices: @@ -22,31 +33,31 @@ async def async_get_service(hass, config, discovery_info=None): class KNXNotificationService(BaseNotificationService): """Implement demo notification service.""" - def __init__(self, devices: list[XknxNotification]): + def __init__(self, devices: list[XknxNotification]) -> None: """Initialize the service.""" self.devices = devices @property - def targets(self): + def targets(self) -> dict[str, str]: """Return a dictionary of registered targets.""" ret = {} for device in self.devices: ret[device.name] = device.name return ret - async def async_send_message(self, message="", **kwargs): + async def async_send_message(self, message: str = "", **kwargs: Any) -> None: """Send a notification to knx bus.""" if "target" in kwargs: await self._async_send_to_device(message, kwargs["target"]) else: await self._async_send_to_all_devices(message) - async def _async_send_to_all_devices(self, message): + async def _async_send_to_all_devices(self, message: str) -> None: """Send a notification to knx bus to all connected devices.""" for device in self.devices: await device.set(message) - async def _async_send_to_device(self, message, names): + async def _async_send_to_device(self, message: str, names: str) -> None: """Send a notification to knx bus to device with given names.""" for device in self.devices: if device.name in names: diff --git a/homeassistant/components/knx/scene.py b/homeassistant/components/knx/scene.py index 6c76fdbd199..bc235da4f35 100644 --- a/homeassistant/components/knx/scene.py +++ b/homeassistant/components/knx/scene.py @@ -1,15 +1,28 @@ """Support for KNX scenes.""" -from typing import Any +from __future__ import annotations + +from typing import Any, Callable, Iterable from xknx.devices import Scene as XknxScene from homeassistant.components.scene import Scene +from homeassistant.helpers.entity import Entity +from homeassistant.helpers.typing import ( + ConfigType, + DiscoveryInfoType, + HomeAssistantType, +) from .const import DOMAIN from .knx_entity import KnxEntity -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +async def async_setup_platform( + hass: HomeAssistantType, + config: ConfigType, + async_add_entities: Callable[[Iterable[Entity]], None], + discovery_info: DiscoveryInfoType | None = None, +) -> None: """Set up the scenes for KNX platform.""" entities = [] for device in hass.data[DOMAIN].xknx.devices: @@ -21,8 +34,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class KNXScene(KnxEntity, Scene): """Representation of a KNX scene.""" - def __init__(self, device: XknxScene): + def __init__(self, device: XknxScene) -> None: """Init KNX scene.""" + self._device: XknxScene super().__init__(device) async def async_activate(self, **kwargs: Any) -> None: diff --git a/homeassistant/components/knx/weather.py b/homeassistant/components/knx/weather.py index 031af9f5af0..c022867284e 100644 --- a/homeassistant/components/knx/weather.py +++ b/homeassistant/components/knx/weather.py @@ -1,16 +1,30 @@ """Support for KNX/IP weather station.""" +from __future__ import annotations + +from typing import Callable, Iterable from xknx.devices import Weather as XknxWeather from homeassistant.components.weather import WeatherEntity from homeassistant.const import TEMP_CELSIUS +from homeassistant.helpers.entity import Entity +from homeassistant.helpers.typing import ( + ConfigType, + DiscoveryInfoType, + HomeAssistantType, +) from .const import DOMAIN from .knx_entity import KnxEntity -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): - """Set up the scenes for KNX platform.""" +async def async_setup_platform( + hass: HomeAssistantType, + config: ConfigType, + async_add_entities: Callable[[Iterable[Entity]], None], + discovery_info: DiscoveryInfoType | None = None, +) -> None: + """Set up weather entities for KNX platform.""" entities = [] for device in hass.data[DOMAIN].xknx.devices: if isinstance(device, XknxWeather): @@ -21,22 +35,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class KNXWeather(KnxEntity, WeatherEntity): """Representation of a KNX weather device.""" - def __init__(self, device: XknxWeather): + def __init__(self, device: XknxWeather) -> None: """Initialize of a KNX sensor.""" + self._device: XknxWeather super().__init__(device) @property - def temperature(self): + def temperature(self) -> float | None: """Return current temperature.""" return self._device.temperature @property - def temperature_unit(self): + def temperature_unit(self) -> str: """Return temperature unit.""" return TEMP_CELSIUS @property - def pressure(self): + def pressure(self) -> float | None: """Return current air pressure.""" # KNX returns pA - HA requires hPa return ( @@ -46,22 +61,22 @@ class KNXWeather(KnxEntity, WeatherEntity): ) @property - def condition(self): + def condition(self) -> str: """Return current weather condition.""" return self._device.ha_current_state().value @property - def humidity(self): + def humidity(self) -> float | None: """Return current humidity.""" return self._device.humidity @property - def wind_bearing(self): + def wind_bearing(self) -> int | None: """Return current wind bearing in degrees.""" return self._device.wind_bearing @property - def wind_speed(self): + def wind_speed(self) -> float | None: """Return current wind speed in km/h.""" # KNX only supports wind speed in m/s return (