Skip to content

Commit

Permalink
set state_class of sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 authored Aug 22, 2022
1 parent 9ebb0e2 commit c4283dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion custom_components/peacefair_energy/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "peacefair_energy",
"name": "Peacefair Energy Monitor",
"version": "v0.7.2",
"version": "v0.7.3",
"config_flow": true,
"documentation": "https://github.com/georgezhao2010/peacefair_energy",
"issue_tracker": "https://github.com/georgezhao2010/peacefair_energy/issue",
Expand Down
28 changes: 16 additions & 12 deletions custom_components/peacefair_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@
_LOGGER = logging.getLogger(__name__)



HPG_SENSORS = {
DEVICE_CLASS_VOLTAGE: {
"name": "Voltage",
"unit": ELECTRIC_POTENTIAL_VOLT,
"state_class": "measurement"
},
DEVICE_CLASS_CURRENT: {
"name": "Current",
"unit": ELECTRIC_CURRENT_AMPERE,
"state_class": "measurement"
},
DEVICE_CLASS_POWER: {
"name": "Power",
"unit": POWER_WATT,
"state_class": "measurement"
},
DEVICE_CLASS_ENERGY: {
"name": "Energy",
Expand All @@ -53,11 +55,13 @@
},
DEVICE_CLASS_POWER_FACTOR: {
"name": "Power Factor",
"state_class": "measurement"
},
DEVICE_CLASS_FREQUENCY: {
"name": "Power Frequency",
"unit": FREQUENCY_HERTZ,
"icon": "hass:current-ac"
"icon": "hass:current-ac",
"state_class": "measurement"
},
}

Expand All @@ -84,10 +88,9 @@
"real_name": "Energy Consumption Today",
}
}

ATTR_LAST_RESET: Final = "last_reset"
ATTR_STATE_CLASS: Final = "state_class"
STATE_CLASS_MEASUREMENT: Final = "measurement"


async def async_setup_entry(hass, config_entry, async_add_entities):
sensors = []
Expand All @@ -102,6 +105,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
state = STATE_UNKNOWN
if len(json_data) > 0:
state = json_data[history_type]["history_state"]
_LOGGER.debug(f"Load {history_type} history data {state}")
h_sensor = HPGHistorySensor(history_type, DEVICE_CLASS_ENERGY, ident, state)
sensors.append(h_sensor)
state = STATE_UNKNOWN
Expand Down Expand Up @@ -169,6 +173,14 @@ def unit_of_measurement(self):
def icon(self):
return HPG_SENSORS[self._sensor_type].get("icon")

@property
def state_class(self):
return HPG_SENSORS[self._sensor_type].get("state_class")

@property
def capability_attributes(self):
return {ATTR_STATE_CLASS: self.state_class} if self.state_class else {}


class HPGHistorySensor(HPGBaseSensor):
def __init__(self, history_type, sensor_type, ident, state):
Expand Down Expand Up @@ -260,14 +272,6 @@ def update_state(self):
json_data[real_type] = self._energy_updates[real_type](cur_time, self.state)
save_json(self.hass.config.path(self._record_file), json_data)

@property
def state_class(self):
return HPG_SENSORS[self._sensor_type].get("state_class")

@property
def capability_attributes(self):
return {ATTR_STATE_CLASS: self.state_class} if self._sensor_type == DEVICE_CLASS_ENERGY else {}

@property
def last_reset(self):
return self._last_reset if self._sensor_type == DEVICE_CLASS_ENERGY else None
Expand Down

0 comments on commit c4283dd

Please sign in to comment.