Skip to content

Commit

Permalink
Use sensor entity base class
Browse files Browse the repository at this point in the history
  • Loading branch information
CJNE committed Oct 1, 2021
1 parent 5d188f7 commit 36fe259
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion custom_components/sunspec/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NAME = "SunSpec"
DOMAIN = "sunspec"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.13"
VERSION = "0.0.16"

ATTRIBUTION = "Data provided by SunSpec alliance - https://sunspec.org"
ISSUE_URL = "https://github.com/cjne/ha-sunspec/issues"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sunspec/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "sunspec",
"name": "SunSpec",
"version": "0.0.15",
"version": "0.0.16",
"documentation": "https://github.com/cjne/ha-sunspec",
"issue_tracker": "https://github.com/cjne/ha-sunspec/issues",
"dependencies": [],
Expand Down
23 changes: 10 additions & 13 deletions custom_components/sunspec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.components.sensor import DEVICE_CLASS_ENERGY
from homeassistant.components.sensor import DEVICE_CLASS_TEMPERATURE
from homeassistant.components.sensor import DEVICE_CLASS_VOLTAGE
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING
from homeassistant.const import DATA_RATE_BITS_PER_SECOND
Expand Down Expand Up @@ -92,7 +93,7 @@ async def async_setup_entry(hass, entry, async_add_devices):
async_add_devices(sensors)


class SunSpecSensor(SunSpecEntity):
class SunSpecSensor(SunSpecEntity, SensorEntity):
"""sunspec Sensor class."""

def __init__(self, coordinator, config_entry, data):
Expand Down Expand Up @@ -206,26 +207,22 @@ def device_class(self):
"""Return de device class of the sensor."""
return self.use_device_class

# @property
# def state_class(self):
# """Return de device class of the sensor."""
# return STATE_CLASS_MEASUREMENT

# @property
# def last_reset(self):
# """Return de device class of the sensor."""
# return 0
@property
def state_class(self):
"""Return de device class of the sensor."""
if self.unit == "":
return None
if self.device_class == DEVICE_CLASS_ENERGY:
return STATE_CLASS_TOTAL_INCREASING
return STATE_CLASS_MEASUREMENT

@property
def device_state_attributes(self):
"""Return the state attributes."""
attrs = {
"integration": DOMAIN,
"sunspec_key": self.key,
"state_class": STATE_CLASS_MEASUREMENT,
}
if self.device_class == DEVICE_CLASS_ENERGY:
attrs["state_class"] = STATE_CLASS_TOTAL_INCREASING
label = self._meta.get("label", None)
if label is not None:
attrs["label"] = label
Expand Down

0 comments on commit 36fe259

Please sign in to comment.