Add strict typing to PVOutput (#62628)

This commit is contained in:
Franck Nijhof 2021-12-23 06:25:05 +01:00 committed by GitHub
parent 36c7521508
commit 79627526c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View File

@ -102,6 +102,7 @@ homeassistant.components.openuv.*
homeassistant.components.persistent_notification.* homeassistant.components.persistent_notification.*
homeassistant.components.pi_hole.* homeassistant.components.pi_hole.*
homeassistant.components.proximity.* homeassistant.components.proximity.*
homeassistant.components.pvoutput.*
homeassistant.components.rainmachine.* homeassistant.components.rainmachine.*
homeassistant.components.rdw.* homeassistant.components.rdw.*
homeassistant.components.recollect_waste.* homeassistant.components.recollect_waste.*

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from pvo import PVOutput, PVOutputError from pvo import PVOutput, PVOutputError, Status
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -20,7 +20,10 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -45,10 +48,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
) )
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the PVOutput sensor.""" """Set up the PVOutput sensor."""
name = config.get(CONF_NAME)
pvoutput = PVOutput( pvoutput = PVOutput(
api_key=config[CONF_API_KEY], api_key=config[CONF_API_KEY],
system_id=config[CONF_SYSTEM_ID], system_id=config[CONF_SYSTEM_ID],
@ -58,9 +64,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
status = await pvoutput.status() status = await pvoutput.status()
except PVOutputError: except PVOutputError:
_LOGGER.error("Unable to fetch data from PVOutput") _LOGGER.error("Unable to fetch data from PVOutput")
return False return
async_add_entities([PvoutputSensor(pvoutput, status, name)]) async_add_entities([PvoutputSensor(pvoutput, status, config[CONF_NAME])])
class PvoutputSensor(SensorEntity): class PvoutputSensor(SensorEntity):
@ -70,19 +76,19 @@ class PvoutputSensor(SensorEntity):
_attr_device_class = SensorDeviceClass.ENERGY _attr_device_class = SensorDeviceClass.ENERGY
_attr_native_unit_of_measurement = ENERGY_WATT_HOUR _attr_native_unit_of_measurement = ENERGY_WATT_HOUR
def __init__(self, pvoutput, status, name): def __init__(self, pvoutput: PVOutput, status: Status, name: str) -> None:
"""Initialize a PVOutput sensor.""" """Initialize a PVOutput sensor."""
self._attr_name = name self._attr_name = name
self.pvoutput = pvoutput self.pvoutput = pvoutput
self.status = status self.status = status
@property @property
def native_value(self): def native_value(self) -> int | None:
"""Return the state of the device.""" """Return the state of the device."""
return self.status.energy_generation return self.status.energy_generation
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, int | float | None]:
"""Return the state attributes of the monitored installation.""" """Return the state attributes of the monitored installation."""
return { return {
ATTR_ENERGY_GENERATION: self.status.energy_generation, ATTR_ENERGY_GENERATION: self.status.energy_generation,
@ -94,6 +100,6 @@ class PvoutputSensor(SensorEntity):
ATTR_VOLTAGE: self.status.voltage, ATTR_VOLTAGE: self.status.voltage,
} }
async def async_update(self): async def async_update(self) -> None:
"""Get the latest data from the PVOutput API and updates the state.""" """Get the latest data from the PVOutput API and updates the state."""
self.status = await self.pvoutput.status() self.status = await self.pvoutput.status()

View File

@ -1133,6 +1133,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.pvoutput.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.rainmachine.*] [mypy-homeassistant.components.rainmachine.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true