Use _attr_ shorthand in greeneye_monitor sensors (#58784)

This commit is contained in:
Jonathan Keljo 2021-11-01 06:25:02 -07:00 committed by GitHub
parent 51873573d3
commit d125dc7dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -117,20 +117,13 @@ class GEMSensor(Generic[T], SensorEntity):
) -> None: ) -> None:
"""Construct the entity.""" """Construct the entity."""
self._monitor_serial_number = monitor_serial_number self._monitor_serial_number = monitor_serial_number
self._name = name self._attr_name = name
self._sensor: T | None = None self._sensor: T | None = None
self._sensor_type = sensor_type self._sensor_type = sensor_type
self._number = number self._number = number
self._attr_unique_id = (
@property f"{self._monitor_serial_number}-{self._sensor_type}-{self._number}"
def unique_id(self) -> str: )
"""Return a unique ID for this sensor."""
return f"{self._monitor_serial_number}-{self._sensor_type}-{self._number}"
@property
def name(self) -> str:
"""Return the name of the channel."""
return self._name
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Wait for and connect to the sensor.""" """Wait for and connect to the sensor."""
@ -223,9 +216,9 @@ class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]):
) -> None: ) -> None:
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "pulse", number) super().__init__(monitor_serial_number, name, "pulse", number)
self._counted_quantity = counted_quantity
self._counted_quantity_per_pulse = counted_quantity_per_pulse self._counted_quantity_per_pulse = counted_quantity_per_pulse
self._time_unit = time_unit self._time_unit = time_unit
self._attr_native_unit_of_measurement = f"{counted_quantity}/{self._time_unit}"
def _get_sensor( def _get_sensor(
self, monitor: greeneye.monitor.Monitor self, monitor: greeneye.monitor.Monitor
@ -260,11 +253,6 @@ class PulseCounter(GEMSensor[greeneye.monitor.PulseCounter]):
f"Invalid value for time unit: {self._time_unit}. Expected one of {TIME_SECONDS}, {TIME_MINUTES}, or {TIME_HOURS}" f"Invalid value for time unit: {self._time_unit}. Expected one of {TIME_SECONDS}, {TIME_MINUTES}, or {TIME_HOURS}"
) )
@property
def native_unit_of_measurement(self) -> str:
"""Return the unit of measurement for this pulse counter."""
return f"{self._counted_quantity}/{self._time_unit}"
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return total pulses in the data dictionary.""" """Return total pulses in the data dictionary."""
@ -284,7 +272,7 @@ class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]):
) -> None: ) -> None:
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "temp", number) super().__init__(monitor_serial_number, name, "temp", number)
self._unit = unit self._attr_native_unit_of_measurement = unit
def _get_sensor( def _get_sensor(
self, monitor: greeneye.monitor.Monitor self, monitor: greeneye.monitor.Monitor
@ -299,11 +287,6 @@ class TemperatureSensor(GEMSensor[greeneye.monitor.TemperatureSensor]):
return cast(Optional[float], self._sensor.temperature) return cast(Optional[float], self._sensor.temperature)
@property
def native_unit_of_measurement(self) -> str:
"""Return the unit of measurement for this sensor (user specified)."""
return self._unit
class VoltageSensor(GEMSensor[greeneye.monitor.Monitor]): class VoltageSensor(GEMSensor[greeneye.monitor.Monitor]):
"""Entity showing voltage.""" """Entity showing voltage."""