Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer user configured entity to autodiscovered one #990

Merged
merged 1 commit into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions custom_components/powercalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ async def autodiscover_entities(config: dict, domain_config: dict, hass: HomeAss
if not await has_manufacturer_and_model_information(hass, entity_entry):
continue

manual_configuration = get_manual_configuration(config, entity_entry.entity_id)
has_user_config = is_user_configured(config, entity_entry.entity_id)

source_entity = await create_source_entity(entity_entry.entity_id, hass)
try:
power_profile = await get_power_profile(
hass, {}, source_entity.entity_entry
)
if power_profile.is_additional_configuration_required:
if not manual_configuration:
if not has_user_config:
_LOGGER.warning(
f"{entity_entry.entity_id}: Model found in database, but needs additional manual configuration to be loaded"
)
Expand All @@ -274,6 +274,13 @@ async def autodiscover_entities(config: dict, domain_config: dict, hass: HomeAss
entity_entry.entity_id,
)
continue

if has_user_config:
_LOGGER.debug(
"%s: Entity is manually configured, skipping auto configuration",
entity_entry.entity_id,
)
continue

if not power_profile:
continue
Expand All @@ -295,12 +302,12 @@ async def autodiscover_entities(config: dict, domain_config: dict, hass: HomeAss
_LOGGER.debug("Done auto discovering entities")


def get_manual_configuration(config: dict, entity_id: str) -> dict | None:
def is_user_configured(config: dict, entity_id: str) -> dict | None:
if SENSOR_DOMAIN not in config:
return None
sensor_config = config.get(SENSOR_DOMAIN)
for item in sensor_config:
if item.get(CONF_PLATFORM) == DOMAIN and item.get(CONF_ENTITY_ID) == entity_id:
if isinstance(item, dict) and item.get(CONF_PLATFORM) == DOMAIN and item.get(CONF_ENTITY_ID) == entity_id:
return item
return None

Expand Down
18 changes: 15 additions & 3 deletions custom_components/powercalc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Final, NamedTuple, Optional, cast

import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.entity_registry as er
import voluptuous as vol
from awesomeversion.awesomeversion import AwesomeVersion
from homeassistant.components import (
Expand Down Expand Up @@ -497,8 +498,8 @@ async def create_individual_sensors(
if (used_unique_ids := hass.data[DOMAIN].get(DATA_USED_UNIQUE_IDS)) is None:
used_unique_ids = hass.data[DOMAIN][DATA_USED_UNIQUE_IDS] = []
try:
check_entity_not_already_configured(
sensor_config, source_entity, hass, used_unique_ids
await check_entity_not_already_configured(
sensor_config, source_entity, hass, used_unique_ids, discovery_info is not None
)
except SensorAlreadyConfiguredError as error:
# Include previously discovered/configured entities in group when no specific configuration
Expand Down Expand Up @@ -583,11 +584,12 @@ async def create_individual_sensors(
return EntitiesBucket(new=entities_to_add, existing=[])


def check_entity_not_already_configured(
async def check_entity_not_already_configured(
sensor_config: dict,
source_entity: SourceEntity,
hass: HomeAssistant,
used_unique_ids: list[str],
is_discovered: True
):
if source_entity.entity_id == DUMMY_ENTITY_ID:
return
Expand All @@ -598,6 +600,16 @@ def check_entity_not_already_configured(
discovered_entities: dict[str, list[SensorEntity]] = hass.data[DOMAIN][
DATA_DISCOVERED_ENTITIES
]

# Prefer configured entity over discovered entity
if not is_discovered and source_entity.entity_id in discovered_entities:
entity_reg = er.async_get(hass)
for entity in discovered_entities.get(source_entity.entity_id):
entity_reg.async_remove(entity.entity_id)
hass.states.async_remove(entity.entity_id)
discovered_entities[source_entity.entity_id] = []
return

existing_entities = (
configured_entities.get(source_entity.entity_id)
or discovered_entities.get(source_entity.entity_id)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CONF_CREATE_UTILITY_METERS,
CONF_ENABLE_AUTODISCOVERY,
CONF_FIXED,
CONF_MANUFACTURER,
CONF_POWER,
CONF_SENSOR_TYPE,
CONF_UTILITY_METER_TYPES,
Expand Down Expand Up @@ -70,6 +71,26 @@ async def test_autodiscovery_disabled(hass: HomeAssistant):

assert not hass.states.get("sensor.testa_power")

async def test_manual_configured_light_overrides_autodiscovered(hass: HomeAssistant):
light_entity = MockLight("testing")
light_entity.manufacturer = "signify"
light_entity.model = "LCA001"
await create_mock_light_entity(hass, light_entity)

await run_powercalc_setup_yaml_config(
hass,
{
CONF_ENTITY_ID: "light.testing",
CONF_FIXED: {
CONF_POWER: 25
}
},
{}
)

state = hass.states.get("sensor.testing_power")
assert state
assert state.state == "25.00"

async def test_domain_groups(hass: HomeAssistant):
await create_input_boolean(hass)
Expand Down