Add some metadata to pvoutput energy sensor (#54074)

pull/54081/head
Paulus Schoutsen 2021-08-05 10:09:56 -07:00 committed by GitHub
parent b930e17d64
commit 3655859be2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 7 deletions

View File

@ -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