Fix some typing issues in greeneye_monitor (#58788)

* Improve typing

* Import cast
pull/58803/head
Jonathan Keljo 2021-10-31 01:30:11 -07:00 committed by GitHub
parent 26b951194c
commit f561533d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 13 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
from greeneye import Monitors from greeneye import Monitors
import voluptuous as vol import voluptuous as vol
@ -18,7 +17,7 @@ from homeassistant.const import (
TIME_MINUTES, TIME_MINUTES,
TIME_SECONDS, TIME_SECONDS,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import Event, HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -130,7 +129,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
server_config = config[DOMAIN] server_config = config[DOMAIN]
server = await monitors.start_server(server_config[CONF_PORT]) server = await monitors.start_server(server_config[CONF_PORT])
async def close_server(*args: list[Any]) -> None: async def close_server(event: Event) -> None:
"""Close the monitoring server.""" """Close the monitoring server."""
await server.close() await server.close()

View File

@ -1,7 +1,7 @@
"""Support for the sensors in a GreenEye Monitor.""" """Support for the sensors in a GreenEye Monitor."""
from __future__ import annotations from __future__ import annotations
from typing import Any, Generic, TypeVar from typing import Any, Generic, Optional, TypeVar, cast
import greeneye import greeneye
from greeneye import Monitors from greeneye import Monitors
@ -140,7 +140,7 @@ class GEMSensor(Generic[T], SensorEntity):
if not self._try_connect_to_monitor(monitors): if not self._try_connect_to_monitor(monitors):
monitors.add_listener(self._on_new_monitor) monitors.add_listener(self._on_new_monitor)
def _on_new_monitor(self, *args: list[Any]) -> None: def _on_new_monitor(self, monitor: greeneye.monitor.Monitor) -> None:
monitors = self.hass.data[DATA_GREENEYE_MONITOR] monitors = self.hass.data[DATA_GREENEYE_MONITOR]
if self._try_connect_to_monitor(monitors): if self._try_connect_to_monitor(monitors):
monitors.remove_listener(self._on_new_monitor) monitors.remove_listener(self._on_new_monitor)
@ -192,8 +192,7 @@ class CurrentSensor(GEMSensor[greeneye.monitor.Channel]):
if not self._sensor: if not self._sensor:
return None return None
assert isinstance(self._sensor.watts, float) return cast(Optional[float], self._sensor.watts)
return self._sensor.watts
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any] | None:
@ -245,8 +244,7 @@ class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]):
* self._counted_quantity_per_pulse * self._counted_quantity_per_pulse
* self._seconds_per_time_unit * self._seconds_per_time_unit
) )
assert isinstance(result, float) return cast(float, result)
return result
@property @property
def _seconds_per_time_unit(self) -> int: def _seconds_per_time_unit(self) -> int:
@ -301,8 +299,7 @@ class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]):
if not self._sensor: if not self._sensor:
return None return None
assert isinstance(self._sensor.temperature, float) return cast(Optional[float], self._sensor.temperature)
return self._sensor.temperature
@property @property
def native_unit_of_measurement(self) -> str: def native_unit_of_measurement(self) -> str:
@ -332,5 +329,4 @@ class VoltageSensor(GEMSensor[greeneye.monitor.Monitor]):
if not self._sensor: if not self._sensor:
return None return None
assert isinstance(self._sensor.voltage, float) return cast(Optional[float], self._sensor.voltage)
return self._sensor.voltage