2021-07-26 15:50:22 +00:00
|
|
|
"""Support for power sensors in WeMo Insight devices."""
|
|
|
|
import asyncio
|
|
|
|
|
2021-07-27 10:05:21 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 08:02:23 +00:00
|
|
|
SensorDeviceClass,
|
2021-07-27 10:05:21 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-16 08:02:23 +00:00
|
|
|
SensorStateClass,
|
2021-07-27 10:05:21 +00:00
|
|
|
)
|
2021-12-20 00:09:30 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-16 08:02:23 +00:00
|
|
|
from homeassistant.const import ENERGY_KILO_WATT_HOUR, POWER_WATT
|
2021-12-20 00:09:30 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-26 15:50:22 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-20 00:09:30 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-07-26 15:50:22 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-08-21 18:14:55 +00:00
|
|
|
from homeassistant.util import convert
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN as WEMO_DOMAIN
|
2021-08-21 18:14:55 +00:00
|
|
|
from .entity import WemoEntity
|
|
|
|
from .wemo_device import DeviceCoordinator
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
|
2021-12-20 00:09:30 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Set up WeMo sensors."""
|
|
|
|
|
2021-12-20 00:09:30 +00:00
|
|
|
async def _discovered_wemo(coordinator: DeviceCoordinator) -> None:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Handle a discovered Wemo device."""
|
|
|
|
async_add_entities(
|
2021-08-21 18:14:55 +00:00
|
|
|
[InsightCurrentPower(coordinator), InsightTodayEnergy(coordinator)]
|
2021-07-26 15:50:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.sensor", _discovered_wemo)
|
|
|
|
|
|
|
|
await asyncio.gather(
|
|
|
|
*(
|
2021-08-21 18:14:55 +00:00
|
|
|
_discovered_wemo(coordinator)
|
|
|
|
for coordinator in hass.data[WEMO_DOMAIN]["pending"].pop("sensor")
|
2021-07-26 15:50:22 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
class InsightSensor(WemoEntity, SensorEntity):
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Common base for WeMo Insight power sensors."""
|
|
|
|
|
|
|
|
@property
|
2021-08-22 18:09:22 +00:00
|
|
|
def name_suffix(self) -> str:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Return the name of the entity if any."""
|
2021-12-21 04:34:34 +00:00
|
|
|
assert self.entity_description.name
|
|
|
|
return self.entity_description.name
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-22 18:09:22 +00:00
|
|
|
def unique_id_suffix(self) -> str:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Return the id of this entity."""
|
2021-08-22 18:09:22 +00:00
|
|
|
return self.entity_description.key
|
2021-07-27 05:18:05 +00:00
|
|
|
|
|
|
|
@property
|
2021-12-20 00:09:30 +00:00
|
|
|
def available(self) -> bool:
|
2021-07-27 05:18:05 +00:00
|
|
|
"""Return true if sensor is available."""
|
|
|
|
return (
|
2021-07-27 10:05:21 +00:00
|
|
|
self.entity_description.key in self.wemo.insight_params
|
|
|
|
and super().available
|
2021-07-27 05:18:05 +00:00
|
|
|
)
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InsightCurrentPower(InsightSensor):
|
|
|
|
"""Current instantaineous power consumption."""
|
|
|
|
|
2021-07-27 10:05:21 +00:00
|
|
|
entity_description = SensorEntityDescription(
|
|
|
|
key="currentpower",
|
|
|
|
name="Current Power",
|
2021-12-16 08:02:23 +00:00
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=POWER_WATT,
|
2021-07-27 10:05:21 +00:00
|
|
|
)
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-15 06:51:43 +00:00
|
|
|
def native_value(self) -> StateType:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Return the current power consumption."""
|
2021-12-20 00:09:30 +00:00
|
|
|
milliwatts = convert(
|
|
|
|
self.wemo.insight_params.get(self.entity_description.key), float, 0.0
|
2021-07-27 05:18:05 +00:00
|
|
|
)
|
2021-12-20 00:09:30 +00:00
|
|
|
assert isinstance(milliwatts, float)
|
|
|
|
return milliwatts / 1000.0
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InsightTodayEnergy(InsightSensor):
|
|
|
|
"""Energy used today."""
|
|
|
|
|
2021-07-27 10:05:21 +00:00
|
|
|
entity_description = SensorEntityDescription(
|
|
|
|
key="todaymw",
|
|
|
|
name="Today Energy",
|
2021-12-16 08:02:23 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-08-20 13:54:57 +00:00
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
2021-07-27 10:05:21 +00:00
|
|
|
)
|
2021-07-26 15:50:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-15 06:51:43 +00:00
|
|
|
def native_value(self) -> StateType:
|
2021-07-26 15:50:22 +00:00
|
|
|
"""Return the current energy use today."""
|
2021-12-20 00:09:30 +00:00
|
|
|
milliwatt_seconds = convert(
|
2021-08-26 08:27:06 +00:00
|
|
|
self.wemo.insight_params.get(self.entity_description.key), float, 0.0
|
2021-07-27 05:18:05 +00:00
|
|
|
)
|
2021-12-20 00:09:30 +00:00
|
|
|
assert isinstance(milliwatt_seconds, float)
|
|
|
|
return round(milliwatt_seconds / (1000.0 * 1000.0 * 60), 2)
|