From 985c62d09e405cc6ebd0e7ce52476734954cd96f Mon Sep 17 00:00:00 2001 From: Bram Date: Fri, 29 Jul 2022 17:20:50 +0200 Subject: [PATCH] Fix correct conversion to template for fixed config entries --- custom_components/powercalc/sensor.py | 2 +- tests/strategy/test_fixed.py | 44 ++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/custom_components/powercalc/sensor.py b/custom_components/powercalc/sensor.py index 73d1483f0..7ef0ebc4c 100644 --- a/custom_components/powercalc/sensor.py +++ b/custom_components/powercalc/sensor.py @@ -317,7 +317,7 @@ def convert_config_entry_to_sensor_config(config_entry: ConfigEntry) -> dict[str if CONF_FIXED in sensor_config: fixed_config = copy.copy(sensor_config.get(CONF_FIXED)) if CONF_POWER_TEMPLATE in fixed_config: - fixed_config[CONF_POWER] = fixed_config[CONF_POWER_TEMPLATE] + fixed_config[CONF_POWER] = Template(fixed_config[CONF_POWER_TEMPLATE]) del fixed_config[CONF_POWER_TEMPLATE] sensor_config[CONF_FIXED] = fixed_config diff --git a/tests/strategy/test_fixed.py b/tests/strategy/test_fixed.py index d9898a78a..ba6b02148 100644 --- a/tests/strategy/test_fixed.py +++ b/tests/strategy/test_fixed.py @@ -1,15 +1,29 @@ import pytest from homeassistant.components import input_number -from homeassistant.const import STATE_ON +from homeassistant.const import STATE_ON, CONF_ENTITY_ID from homeassistant.core import HomeAssistant, State from homeassistant.helpers.event import TrackTemplate from homeassistant.helpers.template import Template from homeassistant.setup import async_setup_component +from custom_components.powercalc.const import ( + CONF_FIXED, + CONF_SENSOR_TYPE, + DOMAIN, + CONF_POWER, + CONF_POWER_TEMPLATE, + SensorType +) from custom_components.powercalc.errors import StrategyConfigurationError from custom_components.powercalc.strategy.fixed import FixedStrategy +from pytest_homeassistant_custom_component.common import MockConfigEntry + from .common import create_source_entity +from ..common import ( + create_input_number, + create_input_boolean +) async def test_simple_power(): @@ -134,3 +148,31 @@ async def test_validation_error_state_power_only_entity_domain(): source_entity=create_source_entity("climate"), ) await strategy.validate_config() + +async def test_config_entry_with_template_rendered_correctly(hass: HomeAssistant): + await create_input_boolean(hass, "test") + await create_input_number(hass, "test", 30) + + template = "{{states('input_number.test')|float}}" + config_entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER, + CONF_ENTITY_ID: "input_boolean.test", + CONF_FIXED: { + CONF_POWER: template, + CONF_POWER_TEMPLATE: template, + } + }, + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + hass.states.async_set("input_boolean.test", STATE_ON) + hass.states.async_set("input_number.test", 40) + await hass.async_block_till_done() + + state = hass.states.get("sensor.test_power") + assert state + assert state.state == "40.00"