Use state class total increasing for TPLink smart plugs (#54723)

pull/54727/head
Michael 2021-08-16 23:54:11 +02:00 committed by GitHub
parent de0460de61
commit 684d035969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 13 deletions

View File

@ -1,7 +1,7 @@
"""Component to embed TP-Link smart home devices."""
from __future__ import annotations
from datetime import datetime, timedelta
from datetime import timedelta
import logging
import time
from typing import Any
@ -11,7 +11,6 @@ from pyHS100.smartplug import SmartPlug
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.sensor import ATTR_LAST_RESET
from homeassistant.components.switch import ATTR_CURRENT_POWER_W, ATTR_TODAY_ENERGY_KWH
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -28,7 +27,6 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.dt import utc_from_timestamp
from .common import SmartDevices, async_discover_devices, get_static_devices
from .const import (
@ -258,12 +256,8 @@ class SmartPlugDataUpdateCoordinator(DataUpdateCoordinator):
ATTR_TOTAL_ENERGY_KWH: round(float(emeter_readings["total"]), 3),
ATTR_VOLTAGE: round(float(emeter_readings["voltage"]), 1),
ATTR_CURRENT_A: round(float(emeter_readings["current"]), 2),
ATTR_LAST_RESET: {ATTR_TOTAL_ENERGY_KWH: utc_from_timestamp(0)},
}
emeter_statics = self.smartplug.get_emeter_daily()
data[CONF_EMETER_PARAMS][ATTR_LAST_RESET][
ATTR_TODAY_ENERGY_KWH
] = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
if emeter_statics.get(int(time.strftime("%e"))):
data[CONF_EMETER_PARAMS][ATTR_TODAY_ENERGY_KWH] = round(
float(emeter_statics[int(time.strftime("%e"))]), 3

View File

@ -6,8 +6,8 @@ from typing import Any, Final
from pyHS100 import SmartPlug
from homeassistant.components.sensor import (
ATTR_LAST_RESET,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
SensorEntityDescription,
)
@ -62,14 +62,14 @@ ENERGY_SENSORS: Final[list[SensorEntityDescription]] = [
key=ATTR_TOTAL_ENERGY_KWH,
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_MEASUREMENT,
state_class=STATE_CLASS_TOTAL_INCREASING,
name="Total Consumption",
),
SensorEntityDescription(
key=ATTR_TODAY_ENERGY_KWH,
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_MEASUREMENT,
state_class=STATE_CLASS_TOTAL_INCREASING,
name="Today's Consumption",
),
SensorEntityDescription(
@ -127,9 +127,6 @@ class SmartPlugSensor(CoordinatorEntity, SensorEntity):
self.smartplug = smartplug
self.entity_description = description
self._attr_name = f"{coordinator.data[CONF_ALIAS]} {description.name}"
self._attr_last_reset = coordinator.data[CONF_EMETER_PARAMS][
ATTR_LAST_RESET
].get(description.key)
@property
def data(self) -> dict[str, Any]: