Enable NUT strict typing (#71913)
This commit is contained in:
parent
9092dcacea
commit
1747061820
5 changed files with 126 additions and 64 deletions
|
@ -1,11 +1,18 @@
|
|||
"""Provides a sensor to track various status aspects of a UPS."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.const import (
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_MODEL,
|
||||
ATTR_SW_VERSION,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -26,9 +33,27 @@ from .const import (
|
|||
STATE_TYPES,
|
||||
)
|
||||
|
||||
NUT_DEV_INFO_TO_DEV_INFO: dict[str, str] = {
|
||||
"manufacturer": ATTR_MANUFACTURER,
|
||||
"model": ATTR_MODEL,
|
||||
"firmware": ATTR_SW_VERSION,
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_nut_device_info(data: PyNUTData) -> DeviceInfo:
|
||||
"""Return a DeviceInfo object filled with NUT device info."""
|
||||
nut_dev_infos = asdict(data.device_info)
|
||||
nut_infos = {
|
||||
info_key: nut_dev_infos[nut_key]
|
||||
for nut_key, info_key in NUT_DEV_INFO_TO_DEV_INFO.items()
|
||||
if nut_dev_infos[nut_key] is not None
|
||||
}
|
||||
|
||||
return cast(DeviceInfo, nut_infos)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
|
@ -64,6 +89,8 @@ async def async_setup_entry(
|
|||
class NUTSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Representation of a sensor entity for NUT status values."""
|
||||
|
||||
coordinator: DataUpdateCoordinator[dict[str, str]]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
|
@ -82,10 +109,10 @@ class NUTSensor(CoordinatorEntity, SensorEntity):
|
|||
identifiers={(DOMAIN, unique_id)},
|
||||
name=device_name,
|
||||
)
|
||||
self._attr_device_info.update(data.device_info)
|
||||
self._attr_device_info.update(_get_nut_device_info(data))
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
def native_value(self) -> str | None:
|
||||
"""Return entity state from ups."""
|
||||
status = self.coordinator.data
|
||||
if self.entity_description.key == KEY_STATUS_DISPLAY:
|
||||
|
@ -93,7 +120,7 @@ class NUTSensor(CoordinatorEntity, SensorEntity):
|
|||
return status.get(self.entity_description.key)
|
||||
|
||||
|
||||
def _format_display_state(status):
|
||||
def _format_display_state(status: dict[str, str]) -> str:
|
||||
"""Return UPS display state."""
|
||||
try:
|
||||
return " ".join(STATE_TYPES[state] for state in status[KEY_STATUS].split())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue