Remove the need for generics in greeneye_monitor.sensor (#58782)

* Remove the need for generics in greeneye_monitor.sensor

* Remove unused imports

* Store monitor and use a property instead
This commit is contained in:
Jonathan Keljo 2021-11-17 20:30:58 -08:00 committed by GitHub
parent e180f1e302
commit 94bfa5272d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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, Optional, TypeVar, cast from typing import Any, Optional, Union, cast
import greeneye import greeneye
@ -97,16 +97,15 @@ async def async_setup_platform(
async_add_entities(entities) async_add_entities(entities)
T = TypeVar( UnderlyingSensorType = Union[
"T",
greeneye.monitor.Channel, greeneye.monitor.Channel,
greeneye.monitor.Monitor, greeneye.monitor.Monitor,
greeneye.monitor.PulseCounter, greeneye.monitor.PulseCounter,
greeneye.monitor.TemperatureSensor, greeneye.monitor.TemperatureSensor,
) ]
class GEMSensor(Generic[T], SensorEntity): class GEMSensor(SensorEntity):
"""Base class for GreenEye Monitor sensors.""" """Base class for GreenEye Monitor sensors."""
_attr_should_poll = False _attr_should_poll = False
@ -117,7 +116,7 @@ class GEMSensor(Generic[T], SensorEntity):
"""Construct the entity.""" """Construct the entity."""
self._monitor_serial_number = monitor_serial_number self._monitor_serial_number = monitor_serial_number
self._attr_name = name self._attr_name = name
self._sensor: T | None = None self._monitor: greeneye.monitor.Monitor | None = None
self._sensor_type = sensor_type self._sensor_type = sensor_type
self._number = number self._number = number
self._attr_unique_id = ( self._attr_unique_id = (
@ -145,21 +144,21 @@ class GEMSensor(Generic[T], SensorEntity):
monitors.remove_listener(self._on_new_monitor) monitors.remove_listener(self._on_new_monitor)
def _try_connect_to_monitor(self, monitors: greeneye.Monitors) -> bool: def _try_connect_to_monitor(self, monitors: greeneye.Monitors) -> bool:
monitor = monitors.monitors.get(self._monitor_serial_number) self._monitor = monitors.monitors.get(self._monitor_serial_number)
if not monitor: if not self._sensor:
return False return False
self._sensor = self._get_sensor(monitor)
self._sensor.add_listener(self.async_write_ha_state) self._sensor.add_listener(self.async_write_ha_state)
self.async_write_ha_state() self.async_write_ha_state()
return True return True
def _get_sensor(self, monitor: greeneye.monitor.Monitor) -> T: @property
def _sensor(self) -> UnderlyingSensorType | None:
raise NotImplementedError() raise NotImplementedError()
class CurrentSensor(GEMSensor[greeneye.monitor.Channel]): class CurrentSensor(GEMSensor):
"""Entity showing power usage on one channel of the monitor.""" """Entity showing power usage on one channel of the monitor."""
_attr_native_unit_of_measurement = UNIT_WATTS _attr_native_unit_of_measurement = UNIT_WATTS
@ -172,10 +171,9 @@ class CurrentSensor(GEMSensor[greeneye.monitor.Channel]):
super().__init__(monitor_serial_number, name, "current", number) super().__init__(monitor_serial_number, name, "current", number)
self._net_metering = net_metering self._net_metering = net_metering
def _get_sensor( @property
self, monitor: greeneye.monitor.Monitor def _sensor(self) -> greeneye.monitor.Channel | None:
) -> greeneye.monitor.Channel: return self._monitor.channels[self._number - 1] if self._monitor else None
return monitor.channels[self._number - 1]
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None:
@ -199,7 +197,7 @@ class CurrentSensor(GEMSensor[greeneye.monitor.Channel]):
return {DATA_WATT_SECONDS: watt_seconds} return {DATA_WATT_SECONDS: watt_seconds}
class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]): class PulseCounter(GEMSensor):
"""Entity showing rate of change in one pulse counter of the monitor.""" """Entity showing rate of change in one pulse counter of the monitor."""
_attr_icon = COUNTER_ICON _attr_icon = COUNTER_ICON
@ -219,10 +217,9 @@ class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]):
self._time_unit = time_unit self._time_unit = time_unit
self._attr_native_unit_of_measurement = f"{counted_quantity}/{self._time_unit}" self._attr_native_unit_of_measurement = f"{counted_quantity}/{self._time_unit}"
def _get_sensor( @property
self, monitor: greeneye.monitor.Monitor def _sensor(self) -> greeneye.monitor.PulseCounter | None:
) -> greeneye.monitor.PulseCounter: return self._monitor.pulse_counters[self._number - 1] if self._monitor else None
return monitor.pulse_counters[self._number - 1]
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None:
@ -261,7 +258,7 @@ class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]):
return {DATA_PULSES: self._sensor.pulses} return {DATA_PULSES: self._sensor.pulses}
class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]): class TemperatureSensor(GEMSensor):
"""Entity showing temperature from one temperature sensor.""" """Entity showing temperature from one temperature sensor."""
_attr_device_class = DEVICE_CLASS_TEMPERATURE _attr_device_class = DEVICE_CLASS_TEMPERATURE
@ -273,10 +270,13 @@ class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]):
super().__init__(monitor_serial_number, name, "temp", number) super().__init__(monitor_serial_number, name, "temp", number)
self._attr_native_unit_of_measurement = unit self._attr_native_unit_of_measurement = unit
def _get_sensor( @property
self, monitor: greeneye.monitor.Monitor def _sensor(self) -> greeneye.monitor.TemperatureSensor | None:
) -> greeneye.monitor.TemperatureSensor: return (
return monitor.temperature_sensors[self._number - 1] self._monitor.temperature_sensors[self._number - 1]
if self._monitor
else None
)
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None:
@ -287,7 +287,7 @@ class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]):
return cast(Optional[float], self._sensor.temperature) return cast(Optional[float], self._sensor.temperature)
class VoltageSensor(GEMSensor[greeneye.monitor.Monitor]): class VoltageSensor(GEMSensor):
"""Entity showing voltage.""" """Entity showing voltage."""
_attr_native_unit_of_measurement = ELECTRIC_POTENTIAL_VOLT _attr_native_unit_of_measurement = ELECTRIC_POTENTIAL_VOLT
@ -297,11 +297,10 @@ class VoltageSensor(GEMSensor[greeneye.monitor.Monitor]):
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "volts", number) super().__init__(monitor_serial_number, name, "volts", number)
def _get_sensor( @property
self, monitor: greeneye.monitor.Monitor def _sensor(self) -> greeneye.monitor.Monitor | None:
) -> greeneye.monitor.Monitor:
"""Wire the updates to the monitor itself, since there is no voltage element in the API.""" """Wire the updates to the monitor itself, since there is no voltage element in the API."""
return monitor return self._monitor
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None: