From 3655859be2c61798d47422875d967cbe11f5fc68 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 5 Aug 2021 10:09:56 -0700 Subject: [PATCH] Add some metadata to pvoutput energy sensor (#54074) --- homeassistant/components/pvoutput/sensor.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/pvoutput/sensor.py b/homeassistant/components/pvoutput/sensor.py index eb461061dcc..999ac14e949 100644 --- a/homeassistant/components/pvoutput/sensor.py +++ b/homeassistant/components/pvoutput/sensor.py @@ -6,7 +6,12 @@ import logging import voluptuous as vol from homeassistant.components.rest.data import RestData -from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity +from homeassistant.components.sensor import ( + DEVICE_CLASS_ENERGY, + PLATFORM_SCHEMA, + STATE_CLASS_MEASUREMENT, + SensorEntity, +) from homeassistant.const import ( ATTR_DATE, ATTR_TEMPERATURE, @@ -14,6 +19,7 @@ from homeassistant.const import ( ATTR_VOLTAGE, CONF_API_KEY, CONF_NAME, + ENERGY_WATT_HOUR, ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv @@ -66,10 +72,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class PvoutputSensor(SensorEntity): """Representation of a PVOutput sensor.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + _attr_device_class = DEVICE_CLASS_ENERGY + _attr_unit_of_measurement = ENERGY_WATT_HOUR + def __init__(self, rest, name): """Initialize a PVOutput sensor.""" self.rest = rest - self._name = name + self._attr_name = name self.pvcoutput = None self.status = namedtuple( "status", @@ -86,11 +96,6 @@ class PvoutputSensor(SensorEntity): ], ) - @property - def name(self): - """Return the name of the sensor.""" - return self._name - @property def state(self): """Return the state of the device.""" @@ -125,6 +130,7 @@ class PvoutputSensor(SensorEntity): def _async_update_from_rest_data(self): """Update state from the rest data.""" try: + # https://pvoutput.org/help/api_specification.html#get-status-service self.pvcoutput = self.status._make(self.rest.data.split(",")) except TypeError: self.pvcoutput = None