Skip to content

Commit

Permalink
Make it possible to define an entity_id for daily-fixed, to inherit n…
Browse files Browse the repository at this point in the history
…ame/entity_id from it
  • Loading branch information
bramstroker committed Aug 21, 2022
1 parent df7bbe3 commit 2d8920c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 3 additions & 6 deletions custom_components/powercalc/sensor.py
Expand Up @@ -393,7 +393,7 @@ def get_merged_sensor_configuration(*configs: dict, validate: bool = True) -> di
CONF_CREATE_ENERGY_SENSORS
)

if CONF_DAILY_FIXED_ENERGY in merged_config:
if CONF_DAILY_FIXED_ENERGY in merged_config and CONF_ENTITY_ID not in merged_config:
merged_config[CONF_ENTITY_ID] = DUMMY_ENTITY_ID

if (
Expand Down Expand Up @@ -423,11 +423,8 @@ async def create_sensors(

global_config = hass.data[DOMAIN][DOMAIN_CONFIG]

if CONF_DAILY_FIXED_ENERGY in config:
config[CONF_ENTITY_ID] = DUMMY_ENTITY_ID

# Setup a power sensor for one single appliance. Either by manual configuration or discovery
if CONF_ENTITY_ID in config or discovery_info is not None:
if CONF_ENTITY_ID in config or discovery_info is not None or CONF_DAILY_FIXED_ENERGY in config:
if discovery_info:
config[CONF_ENTITY_ID] = discovery_info[CONF_ENTITY_ID]
merged_sensor_config = get_merged_sensor_configuration(global_config, config)
Expand Down Expand Up @@ -543,7 +540,7 @@ async def create_individual_sensors(

energy_sensor = None
if CONF_DAILY_FIXED_ENERGY in sensor_config:
energy_sensor = await create_daily_fixed_energy_sensor(hass, sensor_config)
energy_sensor = await create_daily_fixed_energy_sensor(hass, sensor_config, source_entity)
entities_to_add.append(energy_sensor)
power_sensor = await create_daily_fixed_energy_power_sensor(
hass, sensor_config, source_entity
Expand Down
6 changes: 3 additions & 3 deletions custom_components/powercalc/sensors/daily_energy.py
Expand Up @@ -71,14 +71,14 @@


async def create_daily_fixed_energy_sensor(
hass: HomeAssistant, sensor_config: ConfigType
hass: HomeAssistant, sensor_config: ConfigType, source_entity: SourceEntity | None = None
) -> DailyEnergySensor:
mode_config: dict = sensor_config.get(CONF_DAILY_FIXED_ENERGY)

name = generate_energy_sensor_name(sensor_config, sensor_config.get(CONF_NAME))
name = generate_energy_sensor_name(sensor_config, sensor_config.get(CONF_NAME), source_entity)
unique_id = sensor_config.get(CONF_UNIQUE_ID) or None
entity_id = generate_energy_sensor_entity_id(
hass, sensor_config, unique_id=unique_id
hass, sensor_config, unique_id=unique_id, source_entity=source_entity
)
if not unique_id:
old_entity_id = async_generate_entity_id(
Expand Down
17 changes: 17 additions & 0 deletions tests/sensors/test_daily_energy.py
Expand Up @@ -6,6 +6,7 @@
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_UNIT_OF_MEASUREMENT,
CONF_ENTITY_ID,
CONF_NAME,
CONF_PLATFORM,
CONF_UNIT_OF_MEASUREMENT,
Expand Down Expand Up @@ -45,6 +46,7 @@
from ..common import (
assert_entity_state,
create_input_number,
create_input_boolean,
run_powercalc_setup_yaml_config,
)

Expand Down Expand Up @@ -384,6 +386,21 @@ async def test_small_update_frequency_updates_correctly(hass: HomeAssistant):
assert_entity_state(hass, "sensor.router_energy", "0.0100")


async def test_name_and_entity_id_can_be_inherited_from_source_entity(hass: HomeAssistant):
await create_input_boolean(hass, "test")
await run_powercalc_setup_yaml_config(
hass,
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_DAILY_FIXED_ENERGY: {
CONF_VALUE: 0.24,
},
},
)
state = hass.states.get("sensor.test_energy")
assert state


async def _trigger_periodic_update(hass: HomeAssistant, number_of_updates: int = 1):
for i in range(0, number_of_updates):
async_fire_time_changed(
Expand Down

0 comments on commit 2d8920c

Please sign in to comment.