1010from zwave_js_server .model .driver import Driver
1111from zwave_js_server .model .value import Value , get_value_id_str
1212
13- from homeassistant .const import ATTR_DEVICE_ID , ATTR_ENTITY_ID , CONF_PLATFORM , MATCH_ALL
13+ from homeassistant .const import (
14+ ATTR_DEVICE_ID ,
15+ ATTR_ENTITY_ID ,
16+ CONF_OPTIONS ,
17+ CONF_PLATFORM ,
18+ MATCH_ALL ,
19+ )
1420from homeassistant .core import CALLBACK_TYPE , HassJob , HomeAssistant , callback
1521from homeassistant .helpers import config_validation as cv , device_registry as dr
1622from homeassistant .helpers .dispatcher import async_dispatcher_connect
17- from homeassistant .helpers .trigger import Trigger , TriggerActionType , TriggerInfo
23+ from homeassistant .helpers .trigger import (
24+ Trigger ,
25+ TriggerActionType ,
26+ TriggerInfo ,
27+ move_top_level_schema_fields_to_options ,
28+ )
1829from homeassistant .helpers .typing import ConfigType
1930
2031from ..config_validation import VALUE_SCHEMA
4657ATTR_FROM = "from"
4758ATTR_TO = "to"
4859
49- TRIGGER_SCHEMA = vol .All (
50- cv .TRIGGER_BASE_SCHEMA .extend (
51- {
52- vol .Required (CONF_PLATFORM ): PLATFORM_TYPE ,
53- vol .Optional (ATTR_DEVICE_ID ): vol .All (cv .ensure_list , [cv .string ]),
54- vol .Optional (ATTR_ENTITY_ID ): cv .entity_ids ,
55- vol .Required (ATTR_COMMAND_CLASS ): vol .In (
56- {cc .value : cc .name for cc in CommandClass }
57- ),
58- vol .Required (ATTR_PROPERTY ): vol .Any (vol .Coerce (int ), cv .string ),
59- vol .Optional (ATTR_ENDPOINT ): vol .Coerce (int ),
60- vol .Optional (ATTR_PROPERTY_KEY ): vol .Any (vol .Coerce (int ), cv .string ),
61- vol .Optional (ATTR_FROM , default = MATCH_ALL ): vol .Any (
62- VALUE_SCHEMA , [VALUE_SCHEMA ]
63- ),
64- vol .Optional (ATTR_TO , default = MATCH_ALL ): vol .Any (
65- VALUE_SCHEMA , [VALUE_SCHEMA ]
66- ),
67- },
60+ _OPTIONS_SCHEMA_DICT = {
61+ vol .Optional (ATTR_DEVICE_ID ): vol .All (cv .ensure_list , [cv .string ]),
62+ vol .Optional (ATTR_ENTITY_ID ): cv .entity_ids ,
63+ vol .Required (ATTR_COMMAND_CLASS ): vol .In (
64+ {cc .value : cc .name for cc in CommandClass }
6865 ),
69- cv .has_at_least_one_key (ATTR_ENTITY_ID , ATTR_DEVICE_ID ),
66+ vol .Required (ATTR_PROPERTY ): vol .Any (vol .Coerce (int ), cv .string ),
67+ vol .Optional (ATTR_ENDPOINT ): vol .Coerce (int ),
68+ vol .Optional (ATTR_PROPERTY_KEY ): vol .Any (vol .Coerce (int ), cv .string ),
69+ vol .Optional (ATTR_FROM , default = MATCH_ALL ): vol .Any (VALUE_SCHEMA , [VALUE_SCHEMA ]),
70+ vol .Optional (ATTR_TO , default = MATCH_ALL ): vol .Any (VALUE_SCHEMA , [VALUE_SCHEMA ]),
71+ }
72+
73+ _CONFIG_SCHEMA = vol .Schema (
74+ {
75+ vol .Required (CONF_OPTIONS ): vol .All (
76+ _OPTIONS_SCHEMA_DICT ,
77+ cv .has_at_least_one_key (ATTR_ENTITY_ID , ATTR_DEVICE_ID ),
78+ ),
79+ },
7080)
7181
7282
7383async def async_validate_trigger_config (
7484 hass : HomeAssistant , config : ConfigType
7585) -> ConfigType :
7686 """Validate config."""
77- config = TRIGGER_SCHEMA (config )
87+ config = _CONFIG_SCHEMA (config )
88+ options = config [CONF_OPTIONS ]
7889
79- if async_bypass_dynamic_config_validation (hass , config ):
90+ if async_bypass_dynamic_config_validation (hass , options ):
8091 return config
8192
82- if not async_get_nodes_from_targets (hass , config ):
93+ if not async_get_nodes_from_targets (hass , options ):
8394 raise vol .Invalid (
8495 f"No nodes found for given { ATTR_DEVICE_ID } s or { ATTR_ENTITY_ID } s."
8596 )
@@ -88,25 +99,25 @@ async def async_validate_trigger_config(
8899
89100async def async_attach_trigger (
90101 hass : HomeAssistant ,
91- config : ConfigType ,
102+ options : ConfigType ,
92103 action : TriggerActionType ,
93104 trigger_info : TriggerInfo ,
94105 * ,
95106 platform_type : str = PLATFORM_TYPE ,
96107) -> CALLBACK_TYPE :
97108 """Listen for state changes based on configuration."""
98109 dev_reg = dr .async_get (hass )
99- if not async_get_nodes_from_targets (hass , config , dev_reg = dev_reg ):
110+ if not async_get_nodes_from_targets (hass , options , dev_reg = dev_reg ):
100111 raise ValueError (
101112 f"No nodes found for given { ATTR_DEVICE_ID } s or { ATTR_ENTITY_ID } s."
102113 )
103114
104- from_value = config [ATTR_FROM ]
105- to_value = config [ATTR_TO ]
106- command_class = config [ATTR_COMMAND_CLASS ]
107- property_ = config [ATTR_PROPERTY ]
108- endpoint = config .get (ATTR_ENDPOINT )
109- property_key = config .get (ATTR_PROPERTY_KEY )
115+ from_value = options [ATTR_FROM ]
116+ to_value = options [ATTR_TO ]
117+ command_class = options [ATTR_COMMAND_CLASS ]
118+ property_ = options [ATTR_PROPERTY ]
119+ endpoint = options .get (ATTR_ENDPOINT )
120+ property_key = options .get (ATTR_PROPERTY_KEY )
110121 unsubs : list [Callable ] = []
111122 job = HassJob (action )
112123
@@ -174,7 +185,7 @@ def _create_zwave_listeners() -> None:
174185 # Nodes list can come from different drivers and we will need to listen to
175186 # server connections for all of them.
176187 drivers : set [Driver ] = set ()
177- for node in async_get_nodes_from_targets (hass , config , dev_reg = dev_reg ):
188+ for node in async_get_nodes_from_targets (hass , options , dev_reg = dev_reg ):
178189 driver = node .client .driver
179190 assert driver is not None # The node comes from the driver.
180191 drivers .add (driver )
@@ -210,10 +221,16 @@ def _create_zwave_listeners() -> None:
210221class ValueUpdatedTrigger (Trigger ):
211222 """Z-Wave JS value updated trigger."""
212223
213- def __init__ (self , hass : HomeAssistant , config : ConfigType ) -> None :
214- """Initialize trigger."""
215- self ._config = config
216- self ._hass = hass
224+ _hass : HomeAssistant
225+ _options : ConfigType
226+
227+ @classmethod
228+ async def async_validate_complete_config (
229+ cls , hass : HomeAssistant , config : ConfigType
230+ ) -> ConfigType :
231+ """Validate complete config."""
232+ config = move_top_level_schema_fields_to_options (config , _OPTIONS_SCHEMA_DICT )
233+ return await super ().async_validate_complete_config (hass , config )
217234
218235 @classmethod
219236 async def async_validate_config (
@@ -222,12 +239,17 @@ async def async_validate_config(
222239 """Validate config."""
223240 return await async_validate_trigger_config (hass , config )
224241
242+ def __init__ (self , hass : HomeAssistant , config : ConfigType ) -> None :
243+ """Initialize trigger."""
244+ self ._hass = hass
245+ self ._options = config [CONF_OPTIONS ]
246+
225247 async def async_attach (
226248 self ,
227249 action : TriggerActionType ,
228250 trigger_info : TriggerInfo ,
229251 ) -> CALLBACK_TYPE :
230252 """Attach a trigger."""
231253 return await async_attach_trigger (
232- self ._hass , self ._config , action , trigger_info
254+ self ._hass , self ._options , action , trigger_info
233255 )
0 commit comments