2021-07-29 18:02:47 +00:00
|
|
|
"""Support for TPLink HS100/HS110/HS200 smart switch energy sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-07-30 05:11:15 +00:00
|
|
|
from typing import Any, Final
|
2021-07-29 18:02:47 +00:00
|
|
|
|
|
|
|
from pyHS100 import SmartPlug
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
2021-07-30 05:11:15 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-08-16 21:54:11 +00:00
|
|
|
STATE_CLASS_TOTAL_INCREASING,
|
2021-07-29 18:02:47 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.components.tplink import SmartPlugDataUpdateCoordinator
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-07-30 05:11:15 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_VOLTAGE,
|
|
|
|
CONF_ALIAS,
|
|
|
|
CONF_DEVICE_ID,
|
|
|
|
CONF_MAC,
|
|
|
|
DEVICE_CLASS_CURRENT,
|
|
|
|
DEVICE_CLASS_ENERGY,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
DEVICE_CLASS_VOLTAGE,
|
|
|
|
ELECTRIC_CURRENT_AMPERE,
|
|
|
|
ELECTRIC_POTENTIAL_VOLT,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
)
|
2021-07-29 18:02:47 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
import homeassistant.helpers.device_registry as dr
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_EMETER_PARAMS,
|
|
|
|
CONF_MODEL,
|
|
|
|
CONF_SW_VERSION,
|
|
|
|
CONF_SWITCH,
|
|
|
|
COORDINATORS,
|
|
|
|
DOMAIN as TPLINK_DOMAIN,
|
|
|
|
)
|
|
|
|
|
2021-07-30 05:11:15 +00:00
|
|
|
ATTR_CURRENT_A = "current_a"
|
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
|
|
|
ATTR_TODAY_ENERGY_KWH = "today_energy_kwh"
|
|
|
|
ATTR_TOTAL_ENERGY_KWH = "total_energy_kwh"
|
|
|
|
|
|
|
|
ENERGY_SENSORS: Final[list[SensorEntityDescription]] = [
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=ATTR_CURRENT_POWER_W,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=POWER_WATT,
|
2021-07-30 05:11:15 +00:00
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
name="Current Consumption",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=ATTR_TOTAL_ENERGY_KWH,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
2021-07-30 05:11:15 +00:00
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
2021-08-16 21:54:11 +00:00
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
2021-07-30 05:11:15 +00:00
|
|
|
name="Total Consumption",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=ATTR_TODAY_ENERGY_KWH,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
2021-07-30 05:11:15 +00:00
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
2021-08-16 21:54:11 +00:00
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
2021-07-30 05:11:15 +00:00
|
|
|
name="Today's Consumption",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=ATTR_VOLTAGE,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
2021-07-30 05:11:15 +00:00
|
|
|
device_class=DEVICE_CLASS_VOLTAGE,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
name="Voltage",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=ATTR_CURRENT_A,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
2021-07-30 05:11:15 +00:00
|
|
|
device_class=DEVICE_CLASS_CURRENT,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
name="Current",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2021-07-29 18:02:47 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up switches."""
|
|
|
|
entities: list[SmartPlugSensor] = []
|
|
|
|
coordinators: list[SmartPlugDataUpdateCoordinator] = hass.data[TPLINK_DOMAIN][
|
|
|
|
COORDINATORS
|
|
|
|
]
|
|
|
|
switches: list[SmartPlug] = hass.data[TPLINK_DOMAIN][CONF_SWITCH]
|
|
|
|
for switch in switches:
|
2021-08-02 16:47:54 +00:00
|
|
|
coordinator: SmartPlugDataUpdateCoordinator = coordinators[
|
|
|
|
switch.context or switch.mac
|
|
|
|
]
|
2021-07-29 18:02:47 +00:00
|
|
|
if not switch.has_emeter and coordinator.data.get(CONF_EMETER_PARAMS) is None:
|
|
|
|
continue
|
|
|
|
for description in ENERGY_SENSORS:
|
|
|
|
if coordinator.data[CONF_EMETER_PARAMS].get(description.key) is not None:
|
|
|
|
entities.append(SmartPlugSensor(switch, coordinator, description))
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class SmartPlugSensor(CoordinatorEntity, SensorEntity):
|
|
|
|
"""Representation of a TPLink Smart Plug energy sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
smartplug: SmartPlug,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self.smartplug = smartplug
|
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{coordinator.data[CONF_ALIAS]} {description.name}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self) -> dict[str, Any]:
|
|
|
|
"""Return data from DataUpdateCoordinator."""
|
|
|
|
return self.coordinator.data
|
|
|
|
|
|
|
|
@property
|
2021-08-15 06:51:43 +00:00
|
|
|
def native_value(self) -> float | None:
|
2021-07-29 18:02:47 +00:00
|
|
|
"""Return the sensors state."""
|
|
|
|
return self.data[CONF_EMETER_PARAMS][self.entity_description.key]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str | None:
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self.data[CONF_DEVICE_ID]}_{self.entity_description.key}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self) -> DeviceInfo:
|
|
|
|
"""Return information about the device."""
|
|
|
|
return {
|
|
|
|
"name": self.data[CONF_ALIAS],
|
|
|
|
"model": self.data[CONF_MODEL],
|
|
|
|
"manufacturer": "TP-Link",
|
|
|
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self.data[CONF_MAC])},
|
|
|
|
"sw_version": self.data[CONF_SW_VERSION],
|
|
|
|
}
|