1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

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
This commit is contained in:
TheJulianJES 2022-10-02 20:34:15 +02:00 committed by GitHub
parent 069818940e
commit d58e16b990
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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,

View File

@ -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,