mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Clean wemo sensor attributes (#53532)
* Wemo: Use the available property instead of returning unavailable from state property * Nit: s/_insight_device_key/_insight_params_key * Use insight_params_key for generating unique_id
This commit is contained in:
parent
d4d791e0a1
commit
7119285201
@ -9,7 +9,6 @@ from homeassistant.const import (
|
|||||||
DEVICE_CLASS_POWER,
|
DEVICE_CLASS_POWER,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
POWER_WATT,
|
POWER_WATT,
|
||||||
STATE_UNAVAILABLE,
|
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
@ -52,21 +51,14 @@ 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."""
|
||||||
|
|
||||||
def __init__(
|
_attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
self,
|
_name_suffix: str
|
||||||
device: DeviceWrapper,
|
_insight_params_key: str
|
||||||
update_insight_params: Callable,
|
|
||||||
name_suffix: str,
|
def __init__(self, device: DeviceWrapper, update_insight_params: Callable) -> None:
|
||||||
device_class: str,
|
|
||||||
unit_of_measurement: str,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the WeMo Insight power sensor."""
|
"""Initialize the WeMo Insight power sensor."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._update_insight_params = update_insight_params
|
self._update_insight_params = update_insight_params
|
||||||
self._name_suffix = name_suffix
|
|
||||||
self._attr_device_class = device_class
|
|
||||||
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
|
||||||
self._attr_unit_of_measurement = unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
@ -76,7 +68,14 @@ class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
|||||||
@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._name_suffix}"
|
return f"{self.wemo.serialnumber}_{self._insight_params_key}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> str:
|
||||||
|
"""Return true if sensor is available."""
|
||||||
|
return (
|
||||||
|
self._insight_params_key in self.wemo.insight_params and super().available
|
||||||
|
)
|
||||||
|
|
||||||
def _update(self, force_update=True) -> None:
|
def _update(self, force_update=True) -> None:
|
||||||
with self._wemo_exception_handler("update status"):
|
with self._wemo_exception_handler("update status"):
|
||||||
@ -87,36 +86,27 @@ class InsightSensor(WemoSubscriptionEntity, SensorEntity):
|
|||||||
class InsightCurrentPower(InsightSensor):
|
class InsightCurrentPower(InsightSensor):
|
||||||
"""Current instantaineous power consumption."""
|
"""Current instantaineous power consumption."""
|
||||||
|
|
||||||
def __init__(self, device: DeviceWrapper, update_insight_params: Callable) -> None:
|
_attr_device_class = DEVICE_CLASS_POWER
|
||||||
"""Initialize the WeMo Insight power sensor."""
|
_attr_unit_of_measurement = POWER_WATT
|
||||||
super().__init__(
|
_name_suffix = "Current Power"
|
||||||
device,
|
_insight_params_key = "currentpower"
|
||||||
update_insight_params,
|
|
||||||
"Current Power",
|
|
||||||
DEVICE_CLASS_POWER,
|
|
||||||
POWER_WATT,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> StateType:
|
def state(self) -> StateType:
|
||||||
"""Return the current power consumption."""
|
"""Return the current power consumption."""
|
||||||
if "currentpower" not in self.wemo.insight_params:
|
return (
|
||||||
return STATE_UNAVAILABLE
|
convert(self.wemo.insight_params[self._insight_params_key], float, 0.0)
|
||||||
return convert(self.wemo.insight_params["currentpower"], float, 0.0) / 1000.0
|
/ 1000.0
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class InsightTodayEnergy(InsightSensor):
|
class InsightTodayEnergy(InsightSensor):
|
||||||
"""Energy used today."""
|
"""Energy used today."""
|
||||||
|
|
||||||
def __init__(self, device: DeviceWrapper, update_insight_params: Callable) -> None:
|
_attr_device_class = DEVICE_CLASS_ENERGY
|
||||||
"""Initialize the WeMo Insight power sensor."""
|
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
||||||
super().__init__(
|
_name_suffix = "Today Energy"
|
||||||
device,
|
_insight_params_key = "todaymw"
|
||||||
update_insight_params,
|
|
||||||
"Today Energy",
|
|
||||||
DEVICE_CLASS_ENERGY,
|
|
||||||
ENERGY_KILO_WATT_HOUR,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_reset(self) -> datetime:
|
def last_reset(self) -> datetime:
|
||||||
@ -126,7 +116,7 @@ class InsightTodayEnergy(InsightSensor):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> StateType:
|
def state(self) -> StateType:
|
||||||
"""Return the current energy use today."""
|
"""Return the current energy use today."""
|
||||||
if "todaymw" not in self.wemo.insight_params:
|
miliwatts = convert(
|
||||||
return STATE_UNAVAILABLE
|
self.wemo.insight_params[self._insight_params_key], float, 0.0
|
||||||
miliwatts = convert(self.wemo.insight_params["todaymw"], 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