Enable strict typing for arwn (#106840)
This commit is contained in:
parent
bdaf269ba3
commit
4747460286
3 changed files with 48 additions and 24 deletions
|
@ -84,6 +84,7 @@ homeassistant.components.aranet.*
|
||||||
homeassistant.components.arcam_fmj.*
|
homeassistant.components.arcam_fmj.*
|
||||||
homeassistant.components.arris_tg2492lg.*
|
homeassistant.components.arris_tg2492lg.*
|
||||||
homeassistant.components.aruba.*
|
homeassistant.components.aruba.*
|
||||||
|
homeassistant.components.arwn.*
|
||||||
homeassistant.components.aseko_pool_live.*
|
homeassistant.components.aseko_pool_live.*
|
||||||
homeassistant.components.assist_pipeline.*
|
homeassistant.components.assist_pipeline.*
|
||||||
homeassistant.components.asterisk_cdr.*
|
homeassistant.components.asterisk_cdr.*
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components import mqtt
|
from homeassistant.components import mqtt
|
||||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
|
@ -20,7 +21,7 @@ DATA_ARWN = "arwn"
|
||||||
TOPIC = "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.
|
"""Given a topic, dynamically create the right sensor type.
|
||||||
|
|
||||||
Async friendly.
|
Async friendly.
|
||||||
|
@ -34,22 +35,26 @@ def discover_sensors(topic, payload):
|
||||||
unit = UnitOfTemperature.FAHRENHEIT
|
unit = UnitOfTemperature.FAHRENHEIT
|
||||||
else:
|
else:
|
||||||
unit = UnitOfTemperature.CELSIUS
|
unit = UnitOfTemperature.CELSIUS
|
||||||
return ArwnSensor(
|
return [
|
||||||
topic, name, "temp", unit, device_class=SensorDeviceClass.TEMPERATURE
|
ArwnSensor(
|
||||||
)
|
topic, name, "temp", unit, device_class=SensorDeviceClass.TEMPERATURE
|
||||||
|
)
|
||||||
|
]
|
||||||
if domain == "moisture":
|
if domain == "moisture":
|
||||||
name = f"{parts[2]} 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 domain == "rain":
|
||||||
if len(parts) >= 3 and parts[2] == "today":
|
if len(parts) >= 3 and parts[2] == "today":
|
||||||
return ArwnSensor(
|
return [
|
||||||
topic,
|
ArwnSensor(
|
||||||
"Rain Since Midnight",
|
topic,
|
||||||
"since_midnight",
|
"Rain Since Midnight",
|
||||||
UnitOfPrecipitationDepth.INCHES,
|
"since_midnight",
|
||||||
device_class=SensorDeviceClass.PRECIPITATION,
|
UnitOfPrecipitationDepth.INCHES,
|
||||||
)
|
device_class=SensorDeviceClass.PRECIPITATION,
|
||||||
return (
|
)
|
||||||
|
]
|
||||||
|
return [
|
||||||
ArwnSensor(
|
ArwnSensor(
|
||||||
topic + "/total",
|
topic + "/total",
|
||||||
"Total Rainfall",
|
"Total Rainfall",
|
||||||
|
@ -64,11 +69,13 @@ def discover_sensors(topic, payload):
|
||||||
unit,
|
unit,
|
||||||
device_class=SensorDeviceClass.PRECIPITATION,
|
device_class=SensorDeviceClass.PRECIPITATION,
|
||||||
),
|
),
|
||||||
)
|
]
|
||||||
if domain == "barometer":
|
if domain == "barometer":
|
||||||
return ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines")
|
return [
|
||||||
|
ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines")
|
||||||
|
]
|
||||||
if domain == "wind":
|
if domain == "wind":
|
||||||
return (
|
return [
|
||||||
ArwnSensor(
|
ArwnSensor(
|
||||||
topic + "/speed",
|
topic + "/speed",
|
||||||
"Wind Speed",
|
"Wind Speed",
|
||||||
|
@ -86,10 +93,11 @@ def discover_sensors(topic, payload):
|
||||||
ArwnSensor(
|
ArwnSensor(
|
||||||
topic + "/dir", "Wind Direction", "direction", DEGREE, "mdi:compass"
|
topic + "/dir", "Wind Direction", "direction", DEGREE, "mdi:compass"
|
||||||
),
|
),
|
||||||
)
|
]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _slug(name):
|
def _slug(name: str) -> str:
|
||||||
return f"sensor.arwn_{slugify(name)}"
|
return f"sensor.arwn_{slugify(name)}"
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,9 +136,6 @@ async def async_setup_platform(
|
||||||
if (store := hass.data.get(DATA_ARWN)) is None:
|
if (store := hass.data.get(DATA_ARWN)) is None:
|
||||||
store = hass.data[DATA_ARWN] = {}
|
store = hass.data[DATA_ARWN] = {}
|
||||||
|
|
||||||
if isinstance(sensors, ArwnSensor):
|
|
||||||
sensors = (sensors,)
|
|
||||||
|
|
||||||
if "timestamp" in event:
|
if "timestamp" in event:
|
||||||
del event["timestamp"]
|
del event["timestamp"]
|
||||||
|
|
||||||
|
@ -159,7 +164,15 @@ class ArwnSensor(SensorEntity):
|
||||||
|
|
||||||
_attr_should_poll = False
|
_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."""
|
"""Initialize the sensor."""
|
||||||
self.entity_id = _slug(name)
|
self.entity_id = _slug(name)
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
|
@ -170,9 +183,9 @@ class ArwnSensor(SensorEntity):
|
||||||
self._attr_icon = icon
|
self._attr_icon = icon
|
||||||
self._attr_device_class = device_class
|
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."""
|
"""Update the sensor with the most recent event."""
|
||||||
ev = {}
|
ev: dict[str, Any] = {}
|
||||||
ev.update(event)
|
ev.update(event)
|
||||||
self._attr_extra_state_attributes = ev
|
self._attr_extra_state_attributes = ev
|
||||||
self._attr_native_value = ev.get(self._state_key, None)
|
self._attr_native_value = ev.get(self._state_key, None)
|
||||||
|
|
10
mypy.ini
10
mypy.ini
|
@ -600,6 +600,16 @@ disallow_untyped_defs = true
|
||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = 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.*]
|
[mypy-homeassistant.components.aseko_pool_live.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue