1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00

Add unique ID to energy sensors (#70378)

This commit is contained in:
Franck Nijhof 2022-04-27 08:14:16 +02:00 committed by GitHub
parent 3f5027834b
commit 5c1be2f99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 14 deletions

View File

@ -29,7 +29,7 @@ from homeassistant.core import (
split_entity_id,
valid_entity_id,
)
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -210,7 +210,7 @@ class EnergyCostSensor(SensorEntity):
utility.
"""
_attr_entity_category = EntityCategory.SYSTEM
_attr_entity_registry_visible_default = False
_wrong_state_class_reported = False
_wrong_unit_reported = False
@ -416,3 +416,16 @@ class EnergyCostSensor(SensorEntity):
def native_unit_of_measurement(self) -> str | None:
"""Return the units of measurement."""
return self.hass.config.currency
@property
def unique_id(self) -> str | None:
"""Return the unique ID of the sensor."""
entity_registry = er.async_get(self.hass)
if registry_entry := entity_registry.async_get(
self._config[self._adapter.entity_energy_key]
):
prefix = registry_entry.id
else:
prefix = self._config[self._adapter.entity_energy_key]
return f"{prefix}_{self._adapter.source_type}_{self._adapter.entity_id_suffix}"

View File

@ -22,6 +22,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
VOLUME_CUBIC_METERS,
)
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -187,10 +188,12 @@ async def test_cost_sensor_price_entity_total_increasing(
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.TOTAL
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "EUR"
# # Unique ID temp disabled
# # entity_registry = er.async_get(hass)
# # entry = entity_registry.async_get(cost_sensor_entity_id)
# # assert entry.unique_id == "energy_energy_consumption cost"
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(cost_sensor_entity_id)
assert entry
postfix = "cost" if flow_type == "flow_from" else "compensation"
assert entry.unique_id == f"{usage_sensor_entity_id}_grid_{postfix}"
assert entry.hidden_by is er.RegistryEntryHider.INTEGRATION
# Energy use bumped to 10 kWh
hass.states.async_set(
@ -392,10 +395,12 @@ async def test_cost_sensor_price_entity_total(
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.TOTAL
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "EUR"
# # Unique ID temp disabled
# # entity_registry = er.async_get(hass)
# # entry = entity_registry.async_get(cost_sensor_entity_id)
# # assert entry.unique_id == "energy_energy_consumption cost"
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(cost_sensor_entity_id)
assert entry
postfix = "cost" if flow_type == "flow_from" else "compensation"
assert entry.unique_id == f"{usage_sensor_entity_id}_grid_{postfix}"
assert entry.hidden_by is er.RegistryEntryHider.INTEGRATION
# Energy use bumped to 10 kWh
hass.states.async_set(
@ -597,10 +602,12 @@ async def test_cost_sensor_price_entity_total_no_reset(
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.TOTAL
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "EUR"
# # Unique ID temp disabled
# # entity_registry = er.async_get(hass)
# # entry = entity_registry.async_get(cost_sensor_entity_id)
# # assert entry.unique_id == "energy_energy_consumption cost"
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(cost_sensor_entity_id)
assert entry
postfix = "cost" if flow_type == "flow_from" else "compensation"
assert entry.unique_id == f"{usage_sensor_entity_id}_grid_{postfix}"
assert entry.hidden_by is er.RegistryEntryHider.INTEGRATION
# Energy use bumped to 10 kWh
hass.states.async_set(
@ -1018,3 +1025,50 @@ async def test_cost_sensor_state_class_measurement_no_reset(
state = hass.states.get("sensor.energy_consumption_cost")
assert state.state == STATE_UNKNOWN
async def test_inherit_source_unique_id(hass, hass_storage, setup_integration):
"""Test sensor inherits unique ID from source."""
energy_data = data.EnergyManager.default_preferences()
energy_data["energy_sources"].append(
{
"type": "gas",
"stat_energy_from": "sensor.gas_consumption",
"entity_energy_from": "sensor.gas_consumption",
"stat_cost": None,
"entity_energy_price": None,
"number_energy_price": 0.5,
}
)
hass_storage[data.STORAGE_KEY] = {
"version": 1,
"data": energy_data,
}
now = dt_util.utcnow()
entity_registry = er.async_get(hass)
source_entry = entity_registry.async_get_or_create(
"sensor", "test", "123456", suggested_object_id="gas_consumption"
)
hass.states.async_set(
"sensor.gas_consumption",
100,
{
ATTR_UNIT_OF_MEASUREMENT: VOLUME_CUBIC_METERS,
ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING,
},
)
with patch("homeassistant.util.dt.utcnow", return_value=now):
await setup_integration(hass)
state = hass.states.get("sensor.gas_consumption_cost")
assert state
assert state.state == "0.0"
entry = entity_registry.async_get("sensor.gas_consumption_cost")
assert entry
assert entry.unique_id == f"{source_entry.id}_gas_cost"
assert entry.hidden_by is er.RegistryEntryHider.INTEGRATION