From 4747460286fb8d3b9d8c31e2ecd84b0dca3577b6 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 2 Jan 2024 13:07:47 +0100 Subject: [PATCH] Enable strict typing for arwn (#106840) --- .strict-typing | 1 + homeassistant/components/arwn/sensor.py | 61 +++++++++++++++---------- mypy.ini | 10 ++++ 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/.strict-typing b/.strict-typing index b00fd677f3c..ff0ecd1c3ae 100644 --- a/.strict-typing +++ b/.strict-typing @@ -84,6 +84,7 @@ homeassistant.components.aranet.* homeassistant.components.arcam_fmj.* homeassistant.components.arris_tg2492lg.* homeassistant.components.aruba.* +homeassistant.components.arwn.* homeassistant.components.aseko_pool_live.* homeassistant.components.assist_pipeline.* homeassistant.components.asterisk_cdr.* diff --git a/homeassistant/components/arwn/sensor.py b/homeassistant/components/arwn/sensor.py index d468a93eca0..caf7dc6f45e 100644 --- a/homeassistant/components/arwn/sensor.py +++ b/homeassistant/components/arwn/sensor.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +from typing import Any from homeassistant.components import mqtt from homeassistant.components.sensor import SensorDeviceClass, SensorEntity @@ -20,7 +21,7 @@ DATA_ARWN = "arwn" TOPIC = "arwn/#" -def discover_sensors(topic, payload): +def discover_sensors(topic: str, payload: dict[str, Any]) -> list[ArwnSensor] | None: """Given a topic, dynamically create the right sensor type. Async friendly. @@ -34,22 +35,26 @@ def discover_sensors(topic, payload): unit = UnitOfTemperature.FAHRENHEIT else: unit = UnitOfTemperature.CELSIUS - return ArwnSensor( - topic, name, "temp", unit, device_class=SensorDeviceClass.TEMPERATURE - ) + return [ + ArwnSensor( + topic, name, "temp", unit, device_class=SensorDeviceClass.TEMPERATURE + ) + ] if domain == "moisture": name = f"{parts[2]} Moisture" - return ArwnSensor(topic, name, "moisture", unit, "mdi:water-percent") + return [ArwnSensor(topic, name, "moisture", unit, "mdi:water-percent")] if domain == "rain": if len(parts) >= 3 and parts[2] == "today": - return ArwnSensor( - topic, - "Rain Since Midnight", - "since_midnight", - UnitOfPrecipitationDepth.INCHES, - device_class=SensorDeviceClass.PRECIPITATION, - ) - return ( + return [ + ArwnSensor( + topic, + "Rain Since Midnight", + "since_midnight", + UnitOfPrecipitationDepth.INCHES, + device_class=SensorDeviceClass.PRECIPITATION, + ) + ] + return [ ArwnSensor( topic + "/total", "Total Rainfall", @@ -64,11 +69,13 @@ def discover_sensors(topic, payload): unit, device_class=SensorDeviceClass.PRECIPITATION, ), - ) + ] if domain == "barometer": - return ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines") + return [ + ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines") + ] if domain == "wind": - return ( + return [ ArwnSensor( topic + "/speed", "Wind Speed", @@ -86,10 +93,11 @@ def discover_sensors(topic, payload): ArwnSensor( topic + "/dir", "Wind Direction", "direction", DEGREE, "mdi:compass" ), - ) + ] + return None -def _slug(name): +def _slug(name: str) -> str: return f"sensor.arwn_{slugify(name)}" @@ -128,9 +136,6 @@ async def async_setup_platform( if (store := hass.data.get(DATA_ARWN)) is None: store = hass.data[DATA_ARWN] = {} - if isinstance(sensors, ArwnSensor): - sensors = (sensors,) - if "timestamp" in event: del event["timestamp"] @@ -159,7 +164,15 @@ class ArwnSensor(SensorEntity): _attr_should_poll = False - def __init__(self, topic, name, state_key, units, icon=None, device_class=None): + def __init__( + self, + topic: str, + name: str, + state_key: str, + units: str, + icon: str | None = None, + device_class: SensorDeviceClass | None = None, + ) -> None: """Initialize the sensor.""" self.entity_id = _slug(name) self._attr_name = name @@ -170,9 +183,9 @@ class ArwnSensor(SensorEntity): self._attr_icon = icon self._attr_device_class = device_class - def set_event(self, event): + def set_event(self, event: dict[str, Any]) -> None: """Update the sensor with the most recent event.""" - ev = {} + ev: dict[str, Any] = {} ev.update(event) self._attr_extra_state_attributes = ev self._attr_native_value = ev.get(self._state_key, None) diff --git a/mypy.ini b/mypy.ini index 9ffe5e48c1f..f1c2cfa2cf3 100644 --- a/mypy.ini +++ b/mypy.ini @@ -600,6 +600,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.arwn.*] +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.aseko_pool_live.*] check_untyped_defs = true disallow_incomplete_defs = true