Skip to content

Commit 8d27582

Browse files
committed
fix: fixed minimum/maximum targets to support negative values (45 minutes dev time)
1 parent bbaa192 commit 8d27582

File tree

7 files changed

+535
-9
lines changed

7 files changed

+535
-9
lines changed

custom_components/target_timeframes/config/rolling_target_timeframe.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,32 @@ def validate_rolling_target_timeframe_config(data):
8181
if matches is None:
8282
errors[CONFIG_TARGET_OFFSET] = "invalid_offset"
8383

84+
minimum_value: float | None = None
8485
if CONFIG_TARGET_MIN_VALUE in data and data[CONFIG_TARGET_MIN_VALUE] is not None:
8586
if isinstance(data[CONFIG_TARGET_MIN_VALUE], float) == False:
8687
matches = re.search(REGEX_VALUE, data[CONFIG_TARGET_MIN_VALUE])
8788
if matches is None:
8889
errors[CONFIG_TARGET_MIN_VALUE] = "invalid_value"
8990
else:
90-
data[CONFIG_TARGET_MIN_VALUE] = float(data[CONFIG_TARGET_MIN_VALUE])
91+
minimum_value = float(data[CONFIG_TARGET_MIN_VALUE])
92+
data[CONFIG_TARGET_MIN_VALUE] = minimum_value
93+
else:
94+
minimum_value = data[CONFIG_TARGET_MIN_VALUE]
9195

96+
maximum_value: float | None = None
9297
if CONFIG_TARGET_MAX_VALUE in data and data[CONFIG_TARGET_MAX_VALUE] is not None:
9398
if isinstance(data[CONFIG_TARGET_MAX_VALUE], float) == False:
9499
matches = re.search(REGEX_VALUE, data[CONFIG_TARGET_MAX_VALUE])
95100
if matches is None:
96101
errors[CONFIG_TARGET_MAX_VALUE] = "invalid_value"
97102
else:
98-
data[CONFIG_TARGET_MAX_VALUE] = float(data[CONFIG_TARGET_MAX_VALUE])
103+
maximum_value = float(data[CONFIG_TARGET_MAX_VALUE])
104+
data[CONFIG_TARGET_MAX_VALUE] = maximum_value
105+
else:
106+
maximum_value = data[CONFIG_TARGET_MAX_VALUE]
107+
108+
if minimum_value is not None and maximum_value is not None and minimum_value > maximum_value:
109+
errors[CONFIG_TARGET_MIN_VALUE] = "minimum_value_not_less_than_maximum_value"
99110

100111
if CONFIG_TARGET_WEIGHTING in data and data[CONFIG_TARGET_WEIGHTING] is not None:
101112
matches = re.search(REGEX_WEIGHTING, data[CONFIG_TARGET_WEIGHTING])

custom_components/target_timeframes/config/target_timeframe.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,32 @@ def validate_target_timeframe_config(data):
104104
if matches is None:
105105
errors[CONFIG_TARGET_OFFSET] = "invalid_offset"
106106

107+
minimum_value: float | None = None
107108
if CONFIG_TARGET_MIN_VALUE in data and data[CONFIG_TARGET_MIN_VALUE] is not None:
108109
if isinstance(data[CONFIG_TARGET_MIN_VALUE], float) == False:
109110
matches = re.search(REGEX_VALUE, data[CONFIG_TARGET_MIN_VALUE])
110111
if matches is None:
111112
errors[CONFIG_TARGET_MIN_VALUE] = "invalid_value"
112113
else:
113-
data[CONFIG_TARGET_MIN_VALUE] = float(data[CONFIG_TARGET_MIN_VALUE])
114+
minimum_value = float(data[CONFIG_TARGET_MIN_VALUE])
115+
data[CONFIG_TARGET_MIN_VALUE] = minimum_value
116+
else:
117+
minimum_value = data[CONFIG_TARGET_MIN_VALUE]
114118

119+
maximum_value: float | None = None
115120
if CONFIG_TARGET_MAX_VALUE in data and data[CONFIG_TARGET_MAX_VALUE] is not None:
116121
if isinstance(data[CONFIG_TARGET_MAX_VALUE], float) == False:
117122
matches = re.search(REGEX_VALUE, data[CONFIG_TARGET_MAX_VALUE])
118123
if matches is None:
119124
errors[CONFIG_TARGET_MAX_VALUE] = "invalid_value"
120125
else:
121-
data[CONFIG_TARGET_MAX_VALUE] = float(data[CONFIG_TARGET_MAX_VALUE])
126+
maximum_value = float(data[CONFIG_TARGET_MAX_VALUE])
127+
data[CONFIG_TARGET_MAX_VALUE] = maximum_value
128+
else:
129+
maximum_value = data[CONFIG_TARGET_MAX_VALUE]
130+
131+
if minimum_value is not None and maximum_value is not None and minimum_value > maximum_value:
132+
errors[CONFIG_TARGET_MIN_VALUE] = "minimum_value_not_less_than_maximum_value"
122133

123134
if CONFIG_TARGET_WEIGHTING in data and data[CONFIG_TARGET_WEIGHTING] is not None:
124135
matches = re.search(REGEX_WEIGHTING, data[CONFIG_TARGET_WEIGHTING])

custom_components/target_timeframes/config_flow.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from .config.rolling_target_timeframe import merge_rolling_target_timeframe_config, validate_rolling_target_timeframe_config
88

99
from .const import (
10-
CONFIG_DATA_SOURCE_ID,
1110
CONFIG_DATA_UNIQUE_ID,
1211
CONFIG_KIND,
1312
CONFIG_KIND_ROLLING_TARGET_RATE,

custom_components/target_timeframes/const.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@
153153
),
154154
vol.Optional(CONFIG_TARGET_LATEST_VALUES): bool,
155155
vol.Optional(CONFIG_TARGET_FIND_HIGHEST_VALUES): bool,
156-
vol.Optional(CONFIG_TARGET_MIN_VALUE): vol.Coerce(float),
157-
vol.Optional(CONFIG_TARGET_MAX_VALUE): vol.Coerce(float),
156+
vol.Optional(CONFIG_TARGET_MIN_VALUE): float,
157+
vol.Optional(CONFIG_TARGET_MAX_VALUE): float,
158158
vol.Optional(CONFIG_TARGET_WEIGHTING): str,
159159
})
160160

custom_components/target_timeframes/translations/en.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@
9898
"invalid_weighting_slots": "The number of weighting blocks does not equal the specified number of hours.",
9999
"weighting_not_supported_for_type": "Weighting is only supported for continuous target values",
100100
"weighting_not_supported_for_hour_mode": "Weighting is not supported for this hour mode",
101-
"minimum_or_maximum_value_not_specified": "Either minimum and/or maximum value must be specified for minimum hours mode"
101+
"minimum_or_maximum_value_not_specified": "Either minimum and/or maximum value must be specified for minimum hours mode",
102+
"minimum_value_not_less_than_maximum_value": "Minimum value must be less or equal to the maximum value if both are specified"
102103
}
103104
},
104105
"rolling_target_time_period": {
@@ -154,7 +155,8 @@
154155
"invalid_weighting_slots": "The number of weighting blocks does not equal the specified number of hours.",
155156
"weighting_not_supported_for_type": "Weighting is only supported for continuous target values",
156157
"weighting_not_supported_for_hour_mode": "Weighting is not supported for this hour mode",
157-
"minimum_or_maximum_value_not_specified": "Either minimum and/or maximum value must be specified for minimum hours mode"
158+
"minimum_or_maximum_value_not_specified": "Either minimum and/or maximum value must be specified for minimum hours mode",
159+
"minimum_value_not_less_than_maximum_value": "Minimum value must be less or equal to the maximum value if both are specified"
158160
}
159161
}
160162
},

0 commit comments

Comments
 (0)