Use ENERGY device class in oru (#84225)

* Use ENERGY device class in oru

* Apply suggestions from code review

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
pull/84230/head
epenet 2022-12-19 11:41:07 +01:00 committed by GitHub
parent 1f60296415
commit a29aaebd4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 20 deletions

View File

@ -7,8 +7,12 @@ import logging
from oru import Meter, MeterError
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ENERGY_KILO_WATT_HOUR
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.const import UnitOfEnergy
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -20,9 +24,6 @@ CONF_METER_NUMBER = "meter_number"
SCAN_INTERVAL = timedelta(minutes=15)
SENSOR_NAME = "ORU Current Energy Usage"
SENSOR_ICON = "mdi:counter"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_METER_NUMBER): cv.string})
@ -51,12 +52,12 @@ def setup_platform(
class CurrentEnergyUsageSensor(SensorEntity):
"""Representation of the sensor."""
_attr_icon = SENSOR_ICON
_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR
_attr_device_class = SensorDeviceClass.ENERGY
_attr_name = "ORU Current Energy Usage"
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
def __init__(self, meter):
"""Initialize the sensor."""
self._state = None
self._available = None
self.meter = meter
@ -65,26 +66,19 @@ class CurrentEnergyUsageSensor(SensorEntity):
"""Return a unique, Home Assistant friendly identifier for this entity."""
return self.meter.meter_id
@property
def name(self):
"""Return the name of the sensor."""
return SENSOR_NAME
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
def update(self) -> None:
"""Fetch new state data for the sensor."""
try:
last_read = self.meter.last_read()
self._state = last_read
self._attr_native_value = last_read
self._available = True
_LOGGER.debug(
"%s = %s %s", self.name, self._state, self.unit_of_measurement
"%s = %s %s",
self.name,
self.native_value,
self.native_unit_of_measurement,
)
except MeterError as err:
self._available = False