mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Add energy consumption sensors to smartthings devices (#53759)
This commit is contained in:
parent
32c2d42863
commit
8ac1f5d28a
@ -220,6 +220,7 @@ CAPABILITY_TO_SENSORS = {
|
|||||||
Capability.oven_setpoint: [
|
Capability.oven_setpoint: [
|
||||||
Map(Attribute.oven_setpoint, "Oven Set Point", None, None, None)
|
Map(Attribute.oven_setpoint, "Oven Set Point", None, None, None)
|
||||||
],
|
],
|
||||||
|
Capability.power_consumption_report: [],
|
||||||
Capability.power_meter: [
|
Capability.power_meter: [
|
||||||
Map(
|
Map(
|
||||||
Attribute.power,
|
Attribute.power,
|
||||||
@ -388,6 +389,13 @@ CAPABILITY_TO_SENSORS = {
|
|||||||
UNITS = {"C": TEMP_CELSIUS, "F": TEMP_FAHRENHEIT}
|
UNITS = {"C": TEMP_CELSIUS, "F": TEMP_FAHRENHEIT}
|
||||||
|
|
||||||
THREE_AXIS_NAMES = ["X Coordinate", "Y Coordinate", "Z Coordinate"]
|
THREE_AXIS_NAMES = ["X Coordinate", "Y Coordinate", "Z Coordinate"]
|
||||||
|
POWER_CONSUMPTION_REPORT_NAMES = [
|
||||||
|
"energy",
|
||||||
|
"power",
|
||||||
|
"deltaEnergy",
|
||||||
|
"powerEnergy",
|
||||||
|
"energySaved",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
@ -403,6 +411,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
for index in range(len(THREE_AXIS_NAMES))
|
for index in range(len(THREE_AXIS_NAMES))
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
elif capability == Capability.power_consumption_report:
|
||||||
|
sensors.extend(
|
||||||
|
[
|
||||||
|
SmartThingsPowerConsumptionSensor(device, report_name)
|
||||||
|
for report_name in POWER_CONSUMPTION_REPORT_NAMES
|
||||||
|
]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
maps = CAPABILITY_TO_SENSORS[capability]
|
maps = CAPABILITY_TO_SENSORS[capability]
|
||||||
sensors.extend(
|
sensors.extend(
|
||||||
@ -526,3 +541,50 @@ class SmartThingsThreeAxisSensor(SmartThingsEntity, SensorEntity):
|
|||||||
return three_axis[self._index]
|
return three_axis[self._index]
|
||||||
except (TypeError, IndexError):
|
except (TypeError, IndexError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity):
|
||||||
|
"""Define a SmartThings Sensor."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: DeviceEntity,
|
||||||
|
report_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""Init the class."""
|
||||||
|
super().__init__(device)
|
||||||
|
self.report_name = report_name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
"""Return the name of the binary sensor."""
|
||||||
|
return f"{self._device.label} {self.report_name}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Return a unique ID."""
|
||||||
|
return f"{self._device.device_id}.{self.report_name}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
value = self._device.status.attributes[Attribute.power_consumption].value
|
||||||
|
if value.get(self.report_name) is None:
|
||||||
|
return None
|
||||||
|
if self.report_name == "power":
|
||||||
|
return value[self.report_name]
|
||||||
|
return value[self.report_name] / 1000
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self):
|
||||||
|
"""Return the device class of the sensor."""
|
||||||
|
if self.report_name == "power":
|
||||||
|
return DEVICE_CLASS_POWER
|
||||||
|
return DEVICE_CLASS_ENERGY
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
"""Return the unit this state is expressed in."""
|
||||||
|
if self.report_name == "power":
|
||||||
|
return POWER_WATT
|
||||||
|
return ENERGY_KILO_WATT_HOUR
|
||||||
|
@ -139,6 +139,55 @@ async def test_energy_sensors_for_switch_device(hass, device_factory):
|
|||||||
assert entry.manufacturer == "Unavailable"
|
assert entry.manufacturer == "Unavailable"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_power_consumption_sensor(hass, device_factory):
|
||||||
|
"""Test the attributes of the entity are correct."""
|
||||||
|
# Arrange
|
||||||
|
device = device_factory(
|
||||||
|
"refrigerator",
|
||||||
|
[Capability.power_consumption_report],
|
||||||
|
{
|
||||||
|
Attribute.power_consumption: {
|
||||||
|
"energy": 1412002,
|
||||||
|
"deltaEnergy": 25,
|
||||||
|
"power": 109,
|
||||||
|
"powerEnergy": 24.304498331745464,
|
||||||
|
"persistedEnergy": 0,
|
||||||
|
"energySaved": 0,
|
||||||
|
"start": "2021-07-30T16:45:25Z",
|
||||||
|
"end": "2021-07-30T16:58:33Z",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
|
device_registry = dr.async_get(hass)
|
||||||
|
# Act
|
||||||
|
await setup_platform(hass, SENSOR_DOMAIN, devices=[device])
|
||||||
|
# Assert
|
||||||
|
state = hass.states.get("sensor.refrigerator_energy")
|
||||||
|
assert state
|
||||||
|
assert state.state == "1412.002"
|
||||||
|
entry = entity_registry.async_get("sensor.refrigerator_energy")
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == f"{device.device_id}.energy"
|
||||||
|
entry = device_registry.async_get_device({(DOMAIN, device.device_id)})
|
||||||
|
assert entry
|
||||||
|
assert entry.name == device.label
|
||||||
|
assert entry.model == device.device_type_name
|
||||||
|
assert entry.manufacturer == "Unavailable"
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.refrigerator_power")
|
||||||
|
assert state
|
||||||
|
assert state.state == "109"
|
||||||
|
entry = entity_registry.async_get("sensor.refrigerator_power")
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == f"{device.device_id}.power"
|
||||||
|
entry = device_registry.async_get_device({(DOMAIN, device.device_id)})
|
||||||
|
assert entry
|
||||||
|
assert entry.name == device.label
|
||||||
|
assert entry.model == device.device_type_name
|
||||||
|
assert entry.manufacturer == "Unavailable"
|
||||||
|
|
||||||
|
|
||||||
async def test_update_from_signal(hass, device_factory):
|
async def test_update_from_signal(hass, device_factory):
|
||||||
"""Test the binary_sensor updates when receiving a signal."""
|
"""Test the binary_sensor updates when receiving a signal."""
|
||||||
# Arrange
|
# Arrange
|
||||||
|
Loading…
x
Reference in New Issue
Block a user