Enable strict typing for arwn (#106840)

This commit is contained in:
Marc Mueller 2024-01-02 13:07:47 +01:00 committed by GitHub
parent bdaf269ba3
commit 4747460286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 24 deletions

View File

@ -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.*

View File

@ -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)

View File

@ -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