186 files
This commit is contained in:
@@ -12,10 +12,15 @@ import voluptuous as vol
|
||||
|
||||
from custom_components.powercalc.common import SourceEntity
|
||||
from custom_components.powercalc.const import (
|
||||
CONF_CALIBRATE,
|
||||
CONF_COMPOSITE,
|
||||
CONF_GAMMA_CURVE,
|
||||
CONF_MAX_POWER,
|
||||
CONF_MIN_POWER,
|
||||
CONF_MODE,
|
||||
CONF_MULTI_SWITCH,
|
||||
CONF_POWER,
|
||||
CONF_POWER_CURVE,
|
||||
CONF_POWER_OFF,
|
||||
CONF_POWER_TEMPLATE,
|
||||
CONF_STANDBY_POWER,
|
||||
@@ -118,7 +123,7 @@ class PowerCalculatorStrategyFactory:
|
||||
power_profile: PowerProfile | None,
|
||||
) -> LinearStrategy:
|
||||
"""Create the linear strategy."""
|
||||
linear_config = self._get_strategy_config(CalculationStrategy.LINEAR, config, power_profile)
|
||||
linear_config = self._get_linear_config(config, power_profile)
|
||||
|
||||
return LinearStrategy(
|
||||
linear_config,
|
||||
@@ -127,6 +132,55 @@ class PowerCalculatorStrategyFactory:
|
||||
config.get(CONF_STANDBY_POWER),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _get_linear_config(config: ConfigType, power_profile: PowerProfile | None) -> ConfigType:
|
||||
"""Combine an incomplete profile configuration with user supplied power values."""
|
||||
user_config = cast(ConfigType | None, config.get(CalculationStrategy.LINEAR))
|
||||
|
||||
if power_profile is None or not power_profile.is_strategy_supported(CalculationStrategy.LINEAR):
|
||||
if user_config is not None:
|
||||
return user_config
|
||||
raise StrategyConfigurationError("No linear configuration supplied")
|
||||
|
||||
profile_config = dict(power_profile.linear_config or {})
|
||||
|
||||
# Preserve the existing behavior for complete profiles: an explicitly supplied
|
||||
# user configuration replaces the profile configuration entirely.
|
||||
if not power_profile.needs_linear_config:
|
||||
return user_config if user_config is not None else profile_config
|
||||
|
||||
# Smart dimmer profiles without load values can be used from YAML to measure
|
||||
# only the dimmer's own consumption. Keep that behavior when the profile adds
|
||||
# defaults such as gamma_curve but the user omits the linear configuration.
|
||||
if not user_config:
|
||||
return {
|
||||
CONF_MIN_POWER: 0,
|
||||
CONF_MAX_POWER: 0,
|
||||
**profile_config,
|
||||
}
|
||||
|
||||
linear_config = {
|
||||
**profile_config,
|
||||
**user_config,
|
||||
}
|
||||
|
||||
PowerCalculatorStrategyFactory._remove_overridden_profile_curves(linear_config, user_config)
|
||||
|
||||
return linear_config
|
||||
|
||||
@staticmethod
|
||||
def _remove_overridden_profile_curves(linear_config: ConfigType, user_config: ConfigType) -> None:
|
||||
"""Remove profile curve options superseded by explicit user configuration."""
|
||||
if CONF_CALIBRATE in user_config:
|
||||
if CONF_GAMMA_CURVE not in user_config:
|
||||
linear_config.pop(CONF_GAMMA_CURVE, None)
|
||||
if CONF_POWER_CURVE not in user_config:
|
||||
linear_config.pop(CONF_POWER_CURVE, None)
|
||||
elif CONF_GAMMA_CURVE in user_config:
|
||||
linear_config.pop(CONF_POWER_CURVE, None)
|
||||
elif CONF_POWER_CURVE in user_config:
|
||||
linear_config.pop(CONF_GAMMA_CURVE, None)
|
||||
|
||||
def _create_fixed(
|
||||
self,
|
||||
source_entity: SourceEntity,
|
||||
|
||||
Reference in New Issue
Block a user