Skip to content

Commit

Permalink
Keep entity_id the same when the entity has a unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 5, 2022
1 parent 24464bc commit 7fc1c32
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
14 changes: 14 additions & 0 deletions custom_components/powercalc/sensors/abstract.py
Expand Up @@ -2,6 +2,7 @@

from typing import Any

import homeassistant.helpers.entity_registry as er
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback
Expand All @@ -13,6 +14,7 @@
CONF_ENERGY_SENSOR_NAMING,
CONF_POWER_SENSOR_FRIENDLY_NAMING,
CONF_POWER_SENSOR_NAMING,
DOMAIN,
)

ENTITY_ID_FORMAT = SENSOR_DOMAIN + ".{}"
Expand Down Expand Up @@ -73,8 +75,11 @@ def generate_power_sensor_entity_id(
sensor_config: dict[str, Any],
source_entity: SourceEntity | None = None,
name: str | None = None,
unique_id: str | None = None,
) -> str:
"""Generates the entity_id to use for a power sensor"""
if entity_id := get_entity_id_by_unique_id(hass, unique_id):
return entity_id
name_pattern: str = sensor_config.get(CONF_POWER_SENSOR_NAMING)
object_id = name or sensor_config.get(CONF_NAME) or source_entity.object_id
entity_id = async_generate_entity_id(
Expand All @@ -89,11 +94,20 @@ def generate_energy_sensor_entity_id(
sensor_config: dict[str, Any],
source_entity: SourceEntity | None = None,
name: str | None = None,
unique_id: str | None = None,
) -> str:
"""Generates the entity_id to use for an energy sensor"""
if entity_id := get_entity_id_by_unique_id(hass, unique_id):
return entity_id
name_pattern: str = sensor_config.get(CONF_ENERGY_SENSOR_NAMING)
object_id = name or sensor_config.get(CONF_NAME) or source_entity.object_id
entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, name_pattern.format(object_id), hass=hass
)
return entity_id

def get_entity_id_by_unique_id(hass: HomeAssistant, unique_id: str | None) -> str | None:
if unique_id is None:
return None
entity_reg = er.async_get(hass)
return entity_reg.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, unique_id)
18 changes: 10 additions & 8 deletions custom_components/powercalc/sensors/daily_energy.py
Expand Up @@ -76,19 +76,21 @@ async def create_daily_fixed_energy_sensor(
mode_config: dict = sensor_config.get(CONF_DAILY_FIXED_ENERGY)

name = generate_energy_sensor_name(sensor_config, sensor_config.get(CONF_NAME))
entity_id = generate_energy_sensor_entity_id(hass, sensor_config)
old_entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, sensor_config.get(CONF_NAME), hass=hass
)
async_migrate_entity_id(
hass, SENSOR_DOMAIN, old_entity_id=old_entity_id, new_entity_id=entity_id
)
unique_id = sensor_config.get(CONF_UNIQUE_ID) or None
entity_id = generate_energy_sensor_entity_id(hass, sensor_config, unique_id=unique_id)
if not unique_id:
old_entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, sensor_config.get(CONF_NAME), hass=hass
)
async_migrate_entity_id(
hass, SENSOR_DOMAIN, old_entity_id=old_entity_id, new_entity_id=entity_id
)

_LOGGER.debug(
"Creating daily_fixed_energy energy sensor (name=%s, entity_id=%s, unique_id=%s)",
name,
entity_id,
sensor_config.get(CONF_UNIQUE_ID),
unique_id,
)

if CONF_ON_TIME in mode_config:
Expand Down
9 changes: 3 additions & 6 deletions custom_components/powercalc/sensors/energy.py
Expand Up @@ -27,7 +27,6 @@
DEFAULT_ENERGY_INTEGRATION_METHOD,
UnitPrefix,
)
from ..migrate import async_migrate_entity_id
from .abstract import generate_energy_sensor_entity_id, generate_energy_sensor_name
from .power import PowerSensor, RealPowerSensor

Expand Down Expand Up @@ -70,14 +69,12 @@ async def create_energy_sensor(
name = generate_energy_sensor_name(
sensor_config, sensor_config.get(CONF_NAME), source_entity
)
entity_id = generate_energy_sensor_entity_id(hass, sensor_config, source_entity)
entity_category = sensor_config.get(CONF_ENERGY_SENSOR_CATEGORY)
unique_id = None
if power_sensor.unique_id:
unique_id = f"{power_sensor.unique_id}_energy"
async_migrate_entity_id(
hass, SENSOR_DOMAIN, unique_id=unique_id, new_entity_id=entity_id
)

entity_id = generate_energy_sensor_entity_id(hass, sensor_config, source_entity, unique_id=unique_id)
entity_category = sensor_config.get(CONF_ENERGY_SENSOR_CATEGORY)

unit_prefix = sensor_config.get(CONF_ENERGY_SENSOR_UNIT_PREFIX)
if unit_prefix == UnitPrefix.NONE:
Expand Down
10 changes: 2 additions & 8 deletions custom_components/powercalc/sensors/power.py
Expand Up @@ -61,7 +61,6 @@
CalculationStrategy,
)
from ..errors import ModelNotSupported, StrategyConfigurationError, UnsupportedMode
from ..migrate import async_migrate_entity_id
from ..power_profile.model_discovery import get_light_model
from ..strategy.factory import PowerCalculatorStrategyFactory
from ..strategy.strategy_interface import PowerCalculationStrategyInterface
Expand Down Expand Up @@ -98,14 +97,9 @@ async def create_virtual_power_sensor(
name = generate_power_sensor_name(
sensor_config, sensor_config.get(CONF_NAME), source_entity
)
entity_id = generate_power_sensor_entity_id(hass, sensor_config, source_entity)
entity_category = sensor_config.get(CONF_POWER_SENSOR_CATEGORY)

unique_id = sensor_config.get(CONF_UNIQUE_ID) or source_entity.unique_id
if unique_id:
async_migrate_entity_id(
hass, SENSOR_DOMAIN, unique_id=unique_id, new_entity_id=entity_id
)
entity_id = generate_power_sensor_entity_id(hass, sensor_config, source_entity, unique_id=unique_id)
entity_category = sensor_config.get(CONF_POWER_SENSOR_CATEGORY)

light_model = None
try:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_sensor.py
Expand Up @@ -393,3 +393,27 @@ async def test_include_light_group(hass: HomeAssistant):
group_state = hass.states.get("sensor.test_include_lightgroup_power")
assert group_state
assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.bathroom_mirror_power"}

async def test_user_can_rename_entity_id(hass: HomeAssistant, entity_reg: EntityRegistry):
entity_reg.async_get_or_create("sensor", DOMAIN, "abcdef", suggested_object_id="my_renamed_power")
await hass.async_block_till_done()

await create_input_boolean(hass)

await run_powercalc_setup_yaml_config(
hass,
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_MODE: CalculationStrategy.FIXED,
CONF_UNIQUE_ID: "abcdef",
CONF_FIXED: {CONF_POWER: 40},
}
)
await hass.async_block_till_done()

assert hass.states.get("sensor.my_renamed_power")
assert not hass.states.get("sensor.test_power")

energy_state = hass.states.get("sensor.test_energy")
assert energy_state
assert energy_state.attributes.get("source") == "sensor.my_renamed_power"

0 comments on commit 7fc1c32

Please sign in to comment.