Move power and energy attributes to sensors for SmartThings Air conditioner (#72594)

Move power and energy attribute to sensor for Air conditioner
This commit is contained in:
mbo18 2022-06-29 11:02:20 +02:00 committed by GitHub
parent e64336cb91
commit 9d73f9a2c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 20 deletions

View file

@ -106,7 +106,6 @@ def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
Capability.air_conditioner_mode,
Capability.demand_response_load_control,
Capability.air_conditioner_fan_mode,
Capability.power_consumption_report,
Capability.relative_humidity_measurement,
Capability.switch,
Capability.temperature_measurement,
@ -422,10 +421,6 @@ class SmartThingsAirConditioner(SmartThingsEntity, ClimateEntity):
"drlc_status_level",
"drlc_status_start",
"drlc_status_override",
"power_consumption_start",
"power_consumption_power",
"power_consumption_energy",
"power_consumption_end",
]
state_attributes = {}
for attribute in attributes:

View file

@ -553,7 +553,7 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add binary sensors for a config entry."""
"""Add sensors for a config entry."""
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
entities: list[SensorEntity] = []
for device in broker.devices.values():
@ -641,7 +641,7 @@ class SmartThingsSensor(SmartThingsEntity, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the binary sensor."""
"""Return the name of the sensor."""
return f"{self._device.label} {self._name}"
@property
@ -681,7 +681,7 @@ class SmartThingsThreeAxisSensor(SmartThingsEntity, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the binary sensor."""
"""Return the name of the sensor."""
return f"{self._device.label} {THREE_AXIS_NAMES[self._index]}"
@property
@ -716,7 +716,7 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the binary sensor."""
"""Return the name of the sensor."""
return f"{self._device.label} {self.report_name}"
@property
@ -747,3 +747,19 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity):
if self.report_name == "power":
return POWER_WATT
return ENERGY_KILO_WATT_HOUR
@property
def extra_state_attributes(self):
"""Return specific state attributes."""
if self.report_name == "power":
attributes = [
"power_consumption_start",
"power_consumption_end",
]
state_attributes = {}
for attribute in attributes:
value = getattr(self._device.status, attribute)
if value is not None:
state_attributes[attribute] = value
return state_attributes
return None

View file

@ -148,7 +148,6 @@ def air_conditioner_fixture(device_factory):
Capability.air_conditioner_mode,
Capability.demand_response_load_control,
Capability.air_conditioner_fan_mode,
Capability.power_consumption_report,
Capability.switch,
Capability.temperature_measurement,
Capability.thermostat_cooling_setpoint,
@ -177,12 +176,6 @@ def air_conditioner_fixture(device_factory):
"high",
"turbo",
],
Attribute.power_consumption: {
"start": "2019-02-24T21:03:04Z",
"power": 0,
"energy": 500,
"end": "2019-02-26T02:05:55Z",
},
Attribute.switch: "on",
Attribute.cooling_setpoint: 23,
},
@ -320,10 +313,6 @@ async def test_air_conditioner_entity_state(hass, air_conditioner):
assert state.attributes["drlc_status_level"] == -1
assert state.attributes["drlc_status_start"] == "1970-01-01T00:00:00Z"
assert state.attributes["drlc_status_override"] is False
assert state.attributes["power_consumption_start"] == "2019-02-24T21:03:04Z"
assert state.attributes["power_consumption_power"] == 0
assert state.attributes["power_consumption_energy"] == 500
assert state.attributes["power_consumption_end"] == "2019-02-26T02:05:55Z"
async def test_set_fan_mode(hass, thermostat, air_conditioner):

View file

@ -190,6 +190,8 @@ async def test_power_consumption_sensor(hass, device_factory):
state = hass.states.get("sensor.refrigerator_power")
assert state
assert state.state == "109"
assert state.attributes["power_consumption_start"] == "2021-07-30T16:45:25Z"
assert state.attributes["power_consumption_end"] == "2021-07-30T16:58:33Z"
entry = entity_registry.async_get("sensor.refrigerator_power")
assert entry
assert entry.unique_id == f"{device.device_id}.power_meter"