Skip to content

Commit 1b29008

Browse files
authored
Merge pull request #4 from wbyoung/fix-colorint-input
Accept plain integers in advanced LED config list
2 parents be5acd2 + fb953c4 commit 1b29008

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

custom_components/lampie/config_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def _validate_color(
395395
with suppress(ValueError):
396396
color = user_input[CONF_COLOR] = int(color)
397397
try:
398-
Color.color_number(color)
398+
Color.parse_or_validate_in_range(color)
399399
except InvalidColor as e:
400400
errors[CONF_COLOR] = f"invalid_color_{e.reason}"
401401
description_placeholders["color"] = str(color)

custom_components/lampie/types.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Mapping
6+
from contextlib import suppress
67
from dataclasses import dataclass, field
78
import datetime as dt
89
from enum import Enum, IntEnum, StrEnum, auto
@@ -67,8 +68,8 @@ def parse(value: str) -> Color:
6768
return color
6869

6970
@staticmethod
70-
def color_number(value: str | int) -> Color | int:
71-
"""Get a number in the valid color range.
71+
def parse_or_validate_in_range(value: str | int) -> Color | int:
72+
"""Get a color or number in the valid color range.
7273
7374
Validate an input value and convert to a number that's in the valid
7475
color range.
@@ -80,6 +81,9 @@ def color_number(value: str | int) -> Color | int:
8081
InvalidColor: If the color is invalid because it is not a valid
8182
name, is not in range, or is the wrong type.
8283
"""
84+
with suppress(ValueError, TypeError):
85+
value = int(value)
86+
8387
if isinstance(value, str):
8488
return Color.parse(value)
8589
if isinstance(value, int):
@@ -153,9 +157,7 @@ def from_config(config: str | Mapping[str, Any]) -> LEDConfig:
153157
config = {CONF_COLOR: str(config)}
154158

155159
color = config.get(CONF_COLOR, Color.BLUE.name)
156-
color = (
157-
Color.parse(color) if isinstance(color, str) else Color.color_number(color)
158-
)
160+
color = Color.parse_or_validate_in_range(color)
159161
brightness: float = config.get(CONF_BRIGTNESS, 100.0)
160162
effect: Effect = getattr(
161163
Effect, config.get(CONF_EFFECT, Effect.SOLID.name).upper()

tests/test_config_flow.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,40 @@
178178
}
179179
},
180180
},
181+
"valid_led_config_abbreviated": {
182+
"user_input": {
183+
CONF_NAME: "Medicine",
184+
CONF_COLOR: "",
185+
CONF_SWITCH_ENTITIES: ["light.dining_room"],
186+
SECTION_ADVANCED_OPTIONS: {
187+
CONF_LED_CONFIG: [
188+
"red",
189+
"orange",
190+
"white",
191+
250,
192+
20,
193+
255,
194+
0,
195+
]
196+
},
197+
},
198+
"priority_inputs": [],
199+
"expected_result": {
200+
"data": {
201+
CONF_NAME: "Medicine",
202+
CONF_SWITCH_ENTITIES: ["light.dining_room"],
203+
CONF_LED_CONFIG: [
204+
"red",
205+
"orange",
206+
"white",
207+
250,
208+
20,
209+
255,
210+
0,
211+
],
212+
}
213+
},
214+
},
181215
"missing_color": {
182216
"user_input": {
183217
CONF_NAME: "Medicine",

0 commit comments

Comments
 (0)