88 Sequence ,
99 TypedDict ,
1010 cast ,
11+ Protocol ,
1112)
1213from copy import copy
1314
@@ -91,6 +92,12 @@ class IntegerSetting(TypedDict):
9192 default : int | None
9293
9394
95+ class Color (TypedDict ):
96+ red : int
97+ green : int
98+ blue : int
99+
100+
94101class ColorSetting (TypedDict ):
95102 """RGB color setting."""
96103
@@ -99,7 +106,29 @@ class ColorSetting(TypedDict):
99106 title : str
100107 hint : str | None
101108
102- default : Mapping | None
109+ default : Color | None
110+
111+
112+ class CustomSetting (TypedDict ):
113+ """Custom plugin setting.
114+
115+ Can be used for any required custom setting that is not covered by the
116+ default ones (e.g., fan curves, deadzones).
117+
118+ The setting type is defined by family.
119+ Then, the config variable can be used to supply option specific information
120+ (e.g., for fan curves how many temperature points are available).
121+
122+ To validate this setting, each loaded plugin's validate function is called,
123+ with the family, config data, and the supplied value."""
124+
125+ type : Literal ["custom" ]
126+ family : Sequence [str ]
127+ title : str
128+ hint : str | None
129+
130+ config : Any | None
131+ default : Any | None
103132
104133
105134Setting = (
@@ -110,6 +139,7 @@ class ColorSetting(TypedDict):
110139 | NumericalSetting
111140 | IntegerSetting
112141 | ColorSetting
142+ | CustomSetting
113143)
114144
115145#
@@ -244,6 +274,8 @@ def fill_in_defaults(s: Setting | Container | Mode):
244274 case "integer" | "float" :
245275 s ["min" ] = s .get ("min" , None )
246276 s ["max" ] = s .get ("max" , None )
277+ case "custom" :
278+ s ["config" ] = s .get ("config" , None )
247279 return s
248280
249281
@@ -625,7 +657,14 @@ def unravel_options(settings: HHDSettings):
625657 return options
626658
627659
628- def validate_config (conf : Config , settings : HHDSettings , use_defaults : bool = True ):
660+ class Validator (Protocol ):
661+ def __call__ (self , family : Sequence [str ], config : Any , value : Any ) -> bool :
662+ return False
663+
664+
665+ def validate_config (
666+ conf : Config , settings : HHDSettings , validator : Validator , use_defaults : bool = True
667+ ):
629668 options = unravel_options (settings )
630669
631670 for k , d in options .items ():
@@ -667,5 +706,25 @@ def validate_config(conf: Config, settings: HHDSettings, use_defaults: bool = Tr
667706 if v > d ["max" ]:
668707 conf [k ] = d ["max" ]
669708 case "color" :
670- # TODO
671- pass
709+ invalid = False
710+
711+ if not isinstance (v , Mapping ):
712+ invalid = True
713+ else :
714+ for c in ("red" , "green" , "blue" ):
715+ if c not in v :
716+ invalid = True
717+ elif not (0 <= v [c ] < 256 ):
718+ invalid = True
719+
720+ if invalid :
721+ if use_defaults :
722+ conf [k ] = default
723+ else :
724+ del conf [k ]
725+ case "custom" :
726+ if not validator (d ["family" ], d ["config" ], v ):
727+ if use_defaults :
728+ conf [k ] = default
729+ else :
730+ del conf [k ]
0 commit comments