Skip to content

Commit

Permalink
Add utility meter tariff selection to GUI configuration (#2059)
Browse files Browse the repository at this point in the history
* feat: add utility meter tariff selection to GUI configuration

* add translations
  • Loading branch information
bramstroker committed Feb 18, 2024
1 parent b0e8524 commit f66954f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
46 changes: 34 additions & 12 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import selector
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .common import SourceEntity, create_source_entity
from .const import (
Expand Down Expand Up @@ -65,6 +65,7 @@
CONF_SUB_PROFILE,
CONF_UNAVAILABLE_POWER,
CONF_UPDATE_FREQUENCY,
CONF_UTILITY_METER_TARIFFS,
CONF_VALUE,
CONF_VALUE_TEMPLATE,
CONF_WLED,
Expand Down Expand Up @@ -248,15 +249,6 @@
vol.Optional(CONF_UNAVAILABLE_POWER): vol.Coerce(float),
vol.Optional(CONF_MULTIPLY_FACTOR): vol.Coerce(float),
vol.Optional(CONF_MULTIPLY_FACTOR_STANDBY): selector.BooleanSelector(),
vol.Optional(
CONF_ENERGY_INTEGRATION_METHOD,
default=ENERGY_INTEGRATION_METHOD_LEFT,
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=ENERGY_INTEGRATION_METHODS,
mode=selector.SelectSelectorMode.DROPDOWN,
),
),
},
)

Expand All @@ -276,7 +268,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

def __init__(self) -> None:
"""Initialize options flow."""
self.sensor_config: dict[str, Any] = {}
self.sensor_config: ConfigType = {}
self.selected_sensor_type: str | None = None
self.name: str | None = None
self.source_entity: SourceEntity | None = None
Expand Down Expand Up @@ -711,7 +703,7 @@ async def async_step_power_advanced(
return self.async_show_form(
step_id="power_advanced",
data_schema=_fill_schema_defaults(
SCHEMA_POWER_ADVANCED,
_create_schema_advanced(self.sensor_config),
_get_global_powercalc_config(self.hass),
),
errors={},
Expand Down Expand Up @@ -986,6 +978,36 @@ def _create_virtual_power_schema(
return schema.extend(power_options.schema) # type: ignore


def _create_schema_advanced(sensor_config: ConfigType) -> vol.Schema:
schema = SCHEMA_POWER_ADVANCED

if sensor_config.get(CONF_CREATE_ENERGY_SENSOR):
schema = schema.extend(
{
vol.Optional(
CONF_ENERGY_INTEGRATION_METHOD,
default=ENERGY_INTEGRATION_METHOD_LEFT,
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=ENERGY_INTEGRATION_METHODS,
mode=selector.SelectSelectorMode.DROPDOWN,
),
),
},
)

if sensor_config.get(CONF_CREATE_UTILITY_METERS):
schema = schema.extend(
{
vol.Optional(CONF_UTILITY_METER_TARIFFS, default=[]): selector.SelectSelector(
selector.SelectSelectorConfig(options=[], custom_value=True, multiple=True),
),
},
)

return schema


def _create_group_options_schema(
hass: HomeAssistant,
config_entry: ConfigEntry | None = None,
Expand Down
10 changes: 7 additions & 3 deletions custom_components/powercalc/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,16 @@
"ignore_unavailable_state": "Ignore unavailable state",
"multiply_factor": "Multiply factor",
"multiply_factor_standby": "Multiply factor standby",
"unavailable_power": "Unavailable power"
"unavailable_power": "Unavailable power",
"utility_meter_tariffs": "Utility meter tariffs"
},
"data_description": {
"calculation_enabled_condition": "The configured power calculation strategy will only be executed when this template evaluates to true or 1, otherwise the power sensor will display 0",
"ignore_unavailable_state": "Toggle this setting when you want the power sensor to stay available even if the source entity is unavailable",
"multiply_factor": "Multiplies the calculated power by this ratio. Can be useful for light groups",
"multiply_factor_standby": "Whether to also apply multiplication factor to standby power",
"unavailable_power": "Power in W to record when the source entity has an unavailable state"
"unavailable_power": "Power in W to record when the source entity has an unavailable state",
"utility_meter_tariffs": "A list of supported tariffs, leave empty if only a single tariff is needed."
},
"description": "The options below are for advanced powercalc configuration. Most users will not use this so you may skip these",
"title": "Advanced options"
Expand Down Expand Up @@ -260,6 +262,7 @@
"unavailable_power": "Unavailable power",
"unit_of_measurement": "Unit of measurement",
"update_frequency": "Update frequency",
"utility_meter_tariffs": "Utility meter tariffs",
"value": "Value",
"value_template": "Value template"
},
Expand All @@ -281,7 +284,8 @@
"repeat": "Toggle when you want to keep repeating the playbook after it completes",
"states_power": "One 'state: power' pair on each row, see example above",
"sub_groups": "All containing sensors from the selected subgroups will be added to this group as well",
"unavailable_power": "Power in W to record when the source entity has an unavailable state"
"unavailable_power": "Power in W to record when the source entity has an unavailable state",
"utility_meter_tariffs": "A list of supported tariffs, leave empty if only a single tariff is needed."
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/configuration/sensor-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Not all configuration params listed below are available in the GUI, when you wan
+-------------------------------------------+-----------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----+
| utility_meter_offset | string | **Optional** | Define the offset for utility meters. See utility_offset_. | |
+-------------------------------------------+-----------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----+
| utility_meter_tariffs | list | **Optional** | Define different tariffs. See utility_tariffs_. | |
| utility_meter_tariffs | list | **Optional** | Define different tariffs. See utility_tariffs_. | x |
+-------------------------------------------+-----------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----+
| custom_model_directory | string | **Optional** | Directory for a custom light model. Relative from the `config` directory | |
+-------------------------------------------+-----------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----+
Expand Down
42 changes: 42 additions & 0 deletions tests/config_flow/test_utility_meter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from homeassistant import data_entry_flow
from homeassistant.const import CONF_ENTITY_ID, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant

from custom_components.powercalc.const import CONF_CREATE_UTILITY_METERS, CONF_MODE, CONF_POWER, CONF_UTILITY_METER_TARIFFS, CalculationStrategy
from tests.config_flow.common import DEFAULT_ENTITY_ID, DEFAULT_UNIQUE_ID, goto_virtual_power_strategy_step


async def test_utility_meter_tariffs(hass: HomeAssistant) -> None:
result = await goto_virtual_power_strategy_step(
hass,
CalculationStrategy.FIXED,
{
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_MODE: CalculationStrategy.FIXED,
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_CREATE_UTILITY_METERS: True,
},
)

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_POWER: 50},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert CONF_UTILITY_METER_TARIFFS in result["data_schema"].schema

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_UTILITY_METER_TARIFFS: ["peak", "offpeak"]},
)

assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY

await hass.async_block_till_done()

tariff_select = hass.states.get("select.test_energy_daily")
assert tariff_select
assert tariff_select.state == "peak"

assert hass.states.get("sensor.test_energy_daily_peak")
assert hass.states.get("sensor.test_energy_daily_offpeak")

0 comments on commit f66954f

Please sign in to comment.