Enable strict typing - wallbox (#59301)

This commit is contained in:
Marc Mueller 2021-11-23 22:30:22 +01:00 committed by GitHub
parent ce369bb336
commit 6089aef072
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 41 deletions

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import cast
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
@ -10,6 +11,8 @@ from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
)
from homeassistant.components.wallbox import WallboxCoordinator
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CURRENT,
@ -21,6 +24,9 @@ from homeassistant.const import (
PERCENTAGE,
POWER_KILO_WATT,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
@ -130,13 +136,15 @@ SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
}
async def async_setup_entry(hass, config, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Create wallbox sensor entities in HASS."""
coordinator = hass.data[DOMAIN][config.entry_id]
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
[
WallboxSensor(coordinator, config, description)
WallboxSensor(coordinator, entry, description)
for ent in coordinator.data
if (description := SENSOR_TYPES.get(ent))
]
@ -147,24 +155,31 @@ class WallboxSensor(CoordinatorEntity, SensorEntity):
"""Representation of the Wallbox portal."""
entity_description: WallboxSensorEntityDescription
coordinator: WallboxCoordinator
def __init__(
self, coordinator, config, description: WallboxSensorEntityDescription
):
self,
coordinator: WallboxCoordinator,
entry: ConfigEntry,
description: WallboxSensorEntityDescription,
) -> None:
"""Initialize a Wallbox sensor."""
super().__init__(coordinator)
self.entity_description = description
self._attr_name = f"{config.title} {description.name}"
self._attr_name = f"{entry.title} {description.name}"
@property
def native_value(self):
def native_value(self) -> StateType:
"""Return the state of the sensor."""
if (sensor_round := self.entity_description.precision) is not None:
try:
return round(
self.coordinator.data[self.entity_description.key], sensor_round
return cast(
StateType,
round(
self.coordinator.data[self.entity_description.key], sensor_round
),
)
except TypeError:
_LOGGER.debug("Cannot format %s", self._attr_name)
return None
return self.coordinator.data[self.entity_description.key]
return cast(StateType, self.coordinator.data[self.entity_description.key])