mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 05:37:44 +00:00
Use state class total increasing for TPLink smart plugs (#54723)
This commit is contained in:
parent
de0460de61
commit
684d035969
@ -1,7 +1,7 @@
|
|||||||
"""Component to embed TP-Link smart home devices."""
|
"""Component to embed TP-Link smart home devices."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -11,7 +11,6 @@ from pyHS100.smartplug import SmartPlug
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
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.components.switch import ATTR_CURRENT_POWER_W, ATTR_TODAY_ENERGY_KWH
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
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.event import async_track_time_interval
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
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 .common import SmartDevices, async_discover_devices, get_static_devices
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -258,12 +256,8 @@ class SmartPlugDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
ATTR_TOTAL_ENERGY_KWH: round(float(emeter_readings["total"]), 3),
|
ATTR_TOTAL_ENERGY_KWH: round(float(emeter_readings["total"]), 3),
|
||||||
ATTR_VOLTAGE: round(float(emeter_readings["voltage"]), 1),
|
ATTR_VOLTAGE: round(float(emeter_readings["voltage"]), 1),
|
||||||
ATTR_CURRENT_A: round(float(emeter_readings["current"]), 2),
|
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()
|
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"))):
|
if emeter_statics.get(int(time.strftime("%e"))):
|
||||||
data[CONF_EMETER_PARAMS][ATTR_TODAY_ENERGY_KWH] = round(
|
data[CONF_EMETER_PARAMS][ATTR_TODAY_ENERGY_KWH] = round(
|
||||||
float(emeter_statics[int(time.strftime("%e"))]), 3
|
float(emeter_statics[int(time.strftime("%e"))]), 3
|
||||||
|
@ -6,8 +6,8 @@ from typing import Any, Final
|
|||||||
from pyHS100 import SmartPlug
|
from pyHS100 import SmartPlug
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
ATTR_LAST_RESET,
|
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
STATE_CLASS_TOTAL_INCREASING,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
@ -62,14 +62,14 @@ ENERGY_SENSORS: Final[list[SensorEntityDescription]] = [
|
|||||||
key=ATTR_TOTAL_ENERGY_KWH,
|
key=ATTR_TOTAL_ENERGY_KWH,
|
||||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
device_class=DEVICE_CLASS_ENERGY,
|
device_class=DEVICE_CLASS_ENERGY,
|
||||||
state_class=STATE_CLASS_MEASUREMENT,
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||||
name="Total Consumption",
|
name="Total Consumption",
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key=ATTR_TODAY_ENERGY_KWH,
|
key=ATTR_TODAY_ENERGY_KWH,
|
||||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
device_class=DEVICE_CLASS_ENERGY,
|
device_class=DEVICE_CLASS_ENERGY,
|
||||||
state_class=STATE_CLASS_MEASUREMENT,
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||||
name="Today's Consumption",
|
name="Today's Consumption",
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -127,9 +127,6 @@ class SmartPlugSensor(CoordinatorEntity, SensorEntity):
|
|||||||
self.smartplug = smartplug
|
self.smartplug = smartplug
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_name = f"{coordinator.data[CONF_ALIAS]} {description.name}"
|
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
|
@property
|
||||||
def data(self) -> dict[str, Any]:
|
def data(self) -> dict[str, Any]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user