diff --git a/homeassistant/components/ozw/websocket_api.py b/homeassistant/components/ozw/websocket_api.py index 482d78bb878..ea6df900907 100644 --- a/homeassistant/components/ozw/websocket_api.py +++ b/homeassistant/components/ozw/websocket_api.py @@ -15,6 +15,7 @@ from openzwavemqtt.util.node import ( set_config_parameter, ) import voluptuous as vol +import voluptuous_serialize from homeassistant.components import websocket_api from homeassistant.core import callback @@ -29,6 +30,7 @@ OZW_INSTANCE = "ozw_instance" NODE_ID = "node_id" PARAMETER = ATTR_CONFIG_PARAMETER VALUE = ATTR_CONFIG_VALUE +SCHEMA = "schema" ATTR_NODE_QUERY_STAGE = "node_query_stage" ATTR_IS_ZWAVE_PLUS = "is_zwave_plus" @@ -106,6 +108,59 @@ def _call_util_function(hass, connection, msg, send_result, function, *args): connection.send_result(msg[ID]) +def _get_config_params(node, *args): + raw_values = get_config_parameters(node) + config_params = [] + + for param in raw_values: + schema = {} + + if param["type"] in ["Byte", "Int", "Short"]: + schema = vol.Schema( + { + vol.Required(param["label"], default=param["value"]): vol.All( + vol.Coerce(int), vol.Range(min=param["min"], max=param["max"]) + ) + } + ) + data = {param["label"]: param["value"]} + + if param["type"] == "List": + + for options in param["options"]: + if options["Label"] == param["value"]: + selected = options + break + + schema = vol.Schema( + { + vol.Required(param["label"],): vol.In( + { + option["Value"]: option["Label"] + for option in param["options"] + } + ) + } + ) + data = {param["label"]: selected["Value"]} + + config_params.append( + { + "type": param["type"], + "label": param["label"], + "parameter": param["parameter"], + "help": param["help"], + "value": param["value"], + "schema": voluptuous_serialize.convert( + schema, custom_serializer=cv.custom_serializer + ), + "data": data, + } + ) + + return config_params + + @websocket_api.websocket_command({vol.Required(TYPE): "ozw/get_instances"}) def websocket_get_instances(hass, connection, msg): """Get a list of OZW instances.""" @@ -213,7 +268,7 @@ def websocket_get_code_slots(hass, connection, msg): ) def websocket_get_config_parameters(hass, connection, msg): """Get a list of configuration parameters for an OZW node instance.""" - _call_util_function(hass, connection, msg, True, get_config_parameters) + _call_util_function(hass, connection, msg, True, _get_config_params) @websocket_api.websocket_command( @@ -245,7 +300,7 @@ def websocket_get_config_parameters(hass, connection, msg): def websocket_set_config_parameter(hass, connection, msg): """Set a config parameter to a node.""" _call_util_function( - hass, connection, msg, False, set_config_parameter, msg[PARAMETER], msg[VALUE] + hass, connection, msg, True, set_config_parameter, msg[PARAMETER], msg[VALUE] ) diff --git a/tests/components/ozw/test_websocket_api.py b/tests/components/ozw/test_websocket_api.py index d4194b0a537..37c6d184c8e 100644 --- a/tests/components/ozw/test_websocket_api.py +++ b/tests/components/ozw/test_websocket_api.py @@ -28,6 +28,7 @@ from homeassistant.components.ozw.websocket_api import ( NODE_ID, OZW_INSTANCE, PARAMETER, + SCHEMA, TYPE, VALUE, ) @@ -152,16 +153,17 @@ async def test_websocket_api(hass, generic_data, hass_ws_client): # Test set config parameter config_param = result[0] + print(config_param) current_val = config_param[ATTR_VALUE] new_val = next( - option["Value"] - for option in config_param[ATTR_OPTIONS] - if option["Label"] != current_val + option[0] + for option in config_param[SCHEMA][0][ATTR_OPTIONS] + if option[0] != current_val ) new_label = next( - option["Label"] - for option in config_param[ATTR_OPTIONS] - if option["Label"] != current_val and option["Value"] != new_val + option[1] + for option in config_param[SCHEMA][0][ATTR_OPTIONS] + if option[1] != current_val and option[0] != new_val ) await client.send_json( {