mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Use SensorEntityDescription for wemo (#53537)
This commit is contained in:
parent
f4a7292f08
commit
4506b41022
@ -3,7 +3,11 @@ import asyncio
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
DEVICE_CLASS_POWER,
|
DEVICE_CLASS_POWER,
|
||||||
@ -51,9 +55,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
||||||
"""Common base for WeMo Insight power sensors."""
|
"""Common base for WeMo Insight power sensors."""
|
||||||
|
|
||||||
_attr_state_class = STATE_CLASS_MEASUREMENT
|
|
||||||
_name_suffix: str
|
_name_suffix: str
|
||||||
_insight_params_key: str
|
|
||||||
|
|
||||||
def __init__(self, device: DeviceWrapper, update_insight_params: Callable) -> None:
|
def __init__(self, device: DeviceWrapper, update_insight_params: Callable) -> None:
|
||||||
"""Initialize the WeMo Insight power sensor."""
|
"""Initialize the WeMo Insight power sensor."""
|
||||||
@ -63,18 +65,19 @@ class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the entity if any."""
|
"""Return the name of the entity if any."""
|
||||||
return f"{self.wemo.name} {self._name_suffix}"
|
return f"{super().name} {self.entity_description.name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return the id of this entity."""
|
"""Return the id of this entity."""
|
||||||
return f"{self.wemo.serialnumber}_{self._insight_params_key}"
|
return f"{super().unique_id}_{self.entity_description.key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> str:
|
def available(self) -> str:
|
||||||
"""Return true if sensor is available."""
|
"""Return true if sensor is available."""
|
||||||
return (
|
return (
|
||||||
self._insight_params_key in self.wemo.insight_params and super().available
|
self.entity_description.key in self.wemo.insight_params
|
||||||
|
and super().available
|
||||||
)
|
)
|
||||||
|
|
||||||
def _update(self, force_update=True) -> None:
|
def _update(self, force_update=True) -> None:
|
||||||
@ -86,16 +89,19 @@ class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
|||||||
class InsightCurrentPower(InsightSensor):
|
class InsightCurrentPower(InsightSensor):
|
||||||
"""Current instantaineous power consumption."""
|
"""Current instantaineous power consumption."""
|
||||||
|
|
||||||
_attr_device_class = DEVICE_CLASS_POWER
|
entity_description = SensorEntityDescription(
|
||||||
_attr_unit_of_measurement = POWER_WATT
|
key="currentpower",
|
||||||
_name_suffix = "Current Power"
|
name="Current Power",
|
||||||
_insight_params_key = "currentpower"
|
device_class=DEVICE_CLASS_POWER,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
unit_of_measurement=POWER_WATT,
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> StateType:
|
def state(self) -> StateType:
|
||||||
"""Return the current power consumption."""
|
"""Return the current power consumption."""
|
||||||
return (
|
return (
|
||||||
convert(self.wemo.insight_params[self._insight_params_key], float, 0.0)
|
convert(self.wemo.insight_params[self.entity_description.key], float, 0.0)
|
||||||
/ 1000.0
|
/ 1000.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -103,10 +109,13 @@ class InsightCurrentPower(InsightSensor):
|
|||||||
class InsightTodayEnergy(InsightSensor):
|
class InsightTodayEnergy(InsightSensor):
|
||||||
"""Energy used today."""
|
"""Energy used today."""
|
||||||
|
|
||||||
_attr_device_class = DEVICE_CLASS_ENERGY
|
entity_description = SensorEntityDescription(
|
||||||
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
key="todaymw",
|
||||||
_name_suffix = "Today Energy"
|
name="Today Energy",
|
||||||
_insight_params_key = "todaymw"
|
device_class=DEVICE_CLASS_ENERGY,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_reset(self) -> datetime:
|
def last_reset(self) -> datetime:
|
||||||
@ -117,6 +126,6 @@ class InsightTodayEnergy(InsightSensor):
|
|||||||
def state(self) -> StateType:
|
def state(self) -> StateType:
|
||||||
"""Return the current energy use today."""
|
"""Return the current energy use today."""
|
||||||
miliwatts = convert(
|
miliwatts = convert(
|
||||||
self.wemo.insight_params[self._insight_params_key], float, 0.0
|
self.wemo.insight_params[self.entity_description.key], float, 0.0
|
||||||
)
|
)
|
||||||
return round(miliwatts / (1000.0 * 1000.0 * 60), 2)
|
return round(miliwatts / (1000.0 * 1000.0 * 60), 2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user