mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add hex color validator (#110846)
This commit is contained in:
parent
70d1bbb20d
commit
67ac60d042
@ -431,6 +431,19 @@ def icon(value: Any) -> str:
|
|||||||
raise vol.Invalid('Icons should be specified in the form "prefix:name"')
|
raise vol.Invalid('Icons should be specified in the form "prefix:name"')
|
||||||
|
|
||||||
|
|
||||||
|
_COLOR_HEX = re.compile(r"^#[0-9A-F]{6}$", re.IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
|
def color_hex(value: Any) -> str:
|
||||||
|
"""Validate a hex color code."""
|
||||||
|
str_value = str(value)
|
||||||
|
|
||||||
|
if not _COLOR_HEX.match(str_value):
|
||||||
|
raise vol.Invalid("Color should be in the format #RRGGBB")
|
||||||
|
|
||||||
|
return str_value
|
||||||
|
|
||||||
|
|
||||||
_TIME_PERIOD_DICT_KEYS = ("days", "hours", "minutes", "seconds", "milliseconds")
|
_TIME_PERIOD_DICT_KEYS = ("days", "hours", "minutes", "seconds", "milliseconds")
|
||||||
|
|
||||||
time_period_dict = vol.All(
|
time_period_dict = vol.All(
|
||||||
|
@ -1647,3 +1647,27 @@ def test_domain() -> None:
|
|||||||
assert cv.domain_key("hue1") == "hue1"
|
assert cv.domain_key("hue1") == "hue1"
|
||||||
assert cv.domain_key("hue 1") == "hue"
|
assert cv.domain_key("hue 1") == "hue"
|
||||||
assert cv.domain_key("hue 1") == "hue"
|
assert cv.domain_key("hue 1") == "hue"
|
||||||
|
|
||||||
|
|
||||||
|
def test_color_hex() -> None:
|
||||||
|
"""Test color validation in hex format."""
|
||||||
|
assert cv.color_hex("#123456") == "#123456"
|
||||||
|
assert cv.color_hex("#FFaaFF") == "#FFaaFF"
|
||||||
|
assert cv.color_hex("#FFFFFF") == "#FFFFFF"
|
||||||
|
assert cv.color_hex("#000000") == "#000000"
|
||||||
|
|
||||||
|
msg = r"Color should be in the format #RRGGBB"
|
||||||
|
with pytest.raises(vol.Invalid, match=msg):
|
||||||
|
cv.color_hex("#777")
|
||||||
|
|
||||||
|
with pytest.raises(vol.Invalid, match=msg):
|
||||||
|
cv.color_hex("FFFFF")
|
||||||
|
|
||||||
|
with pytest.raises(vol.Invalid, match=msg):
|
||||||
|
cv.color_hex("FFFFFF")
|
||||||
|
|
||||||
|
with pytest.raises(vol.Invalid, match=msg):
|
||||||
|
cv.color_hex("#FFFFFFF")
|
||||||
|
|
||||||
|
with pytest.raises(vol.Invalid, match=msg):
|
||||||
|
cv.color_hex(123456)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user