From 43891f0baa02e6e207361431b75785665fc8e45b Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Sun, 2 Oct 2022 20:34:15 +0200 Subject: [PATCH] Fix empty default ZHA configuration (#79475) * Also add 0 as a default for transition in const.py This is the same default transition (none) that is used in ZHA's light.py * Send default values for unconfigured options in ZHA's configuration API * Remove options that match defaults values before saving --- homeassistant/components/zha/api.py | 22 ++++++++++++++++++++++ homeassistant/components/zha/core/const.py | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zha/api.py b/homeassistant/components/zha/api.py index 1095bae5ac8..6cbcdf50983 100644 --- a/homeassistant/components/zha/api.py +++ b/homeassistant/components/zha/api.py @@ -1028,6 +1028,12 @@ async def websocket_get_configuration( data["data"][section] = zha_gateway.config_entry.options.get( CUSTOM_CONFIGURATION, {} ).get(section, {}) + + # send default values for unconfigured options + for entry in data["schemas"][section]: + if data["data"][section].get(entry["name"]) is None: + data["data"][section][entry["name"]] = entry["default"] + connection.send_result(msg[ID], data) @@ -1047,6 +1053,22 @@ async def websocket_update_zha_configuration( options = zha_gateway.config_entry.options data_to_save = {**options, **{CUSTOM_CONFIGURATION: msg["data"]}} + for section, schema in ZHA_CONFIG_SCHEMAS.items(): + for entry in schema.schema: + # remove options that match defaults + if ( + data_to_save[CUSTOM_CONFIGURATION].get(section, {}).get(entry) + == entry.default() + ): + data_to_save[CUSTOM_CONFIGURATION][section].pop(entry) + # remove entire section block if empty + if not data_to_save[CUSTOM_CONFIGURATION][section]: + data_to_save[CUSTOM_CONFIGURATION].pop(section) + + # remove entire custom_configuration block if empty + if not data_to_save[CUSTOM_CONFIGURATION]: + data_to_save.pop(CUSTOM_CONFIGURATION) + _LOGGER.info( "Updating ZHA custom configuration options from %s to %s", options, diff --git a/homeassistant/components/zha/core/const.py b/homeassistant/components/zha/core/const.py index 0204fb50bed..b9871a1f2ab 100644 --- a/homeassistant/components/zha/core/const.py +++ b/homeassistant/components/zha/core/const.py @@ -146,7 +146,7 @@ CONF_DEFAULT_CONSIDER_UNAVAILABLE_BATTERY = 60 * 60 * 6 # 6 hours CONF_ZHA_OPTIONS_SCHEMA = vol.Schema( { - vol.Optional(CONF_DEFAULT_LIGHT_TRANSITION): cv.positive_int, + vol.Optional(CONF_DEFAULT_LIGHT_TRANSITION, default=0): cv.positive_int, vol.Required(CONF_ENABLE_ENHANCED_LIGHT_TRANSITION, default=False): cv.boolean, vol.Required(CONF_ENABLE_LIGHT_TRANSITIONING_FLAG, default=True): cv.boolean, vol.Required(CONF_ALWAYS_PREFER_XY_COLOR_MODE, default=True): cv.boolean,