From 762ddce13f4d7db4d0a46bdb3f27d45ef8e6c066 Mon Sep 17 00:00:00 2001 From: Alone Date: Wed, 19 Feb 2025 16:26:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=90=20add=20yaml=20mode=20for=20custom?= =?UTF-8?q?izing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/config_flow.py | 64 ++++++++++++------- .../xiaomi_miot/translations/en.json | 2 + .../xiaomi_miot/translations/zh-Hans.json | 2 + .../xiaomi_miot/translations/zh-Hant.json | 2 + 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/custom_components/xiaomi_miot/config_flow.py b/custom_components/xiaomi_miot/config_flow.py index 740154042d..ad6e2a03b3 100644 --- a/custom_components/xiaomi_miot/config_flow.py +++ b/custom_components/xiaomi_miot/config_flow.py @@ -18,8 +18,9 @@ from homeassistant.core import callback, split_entity_id from homeassistant.util import yaml from homeassistant.components import persistent_notification +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.device_registry import format_mac -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.selector import ObjectSelector from . import ( DOMAIN, @@ -444,16 +445,23 @@ async def async_step_customizing(self, user_input=None): if last_step and customize_key: reset = user_input.pop('reset_customizes', None) b2s = user_input.pop('bool2selects', None) or [] - for k in b2s: - user_input[k] = True - entry_data.setdefault(via, {}) - entry_data[via][customize_key] = { - k: v - for k, v in user_input.items() - if v not in [' ', '', None, vol.UNDEFINED] - } + customize_data = entry_data.setdefault(via, {}) if reset: entry_data[via].pop(customize_key, None) + elif self.context.get('yaml_mode'): + yml = user_input.get('yaml_customizes') or {} + if yml: + customize_data[customize_key] = yml + else: + entry_data[via].pop(customize_key, None) + else: + for k in b2s: + user_input[k] = True + customize_data[customize_key] = { + k: v + for k, v in user_input.items() + if v not in [' ', '', None, vol.UNDEFINED] + } if entry: self.hass.config_entries.async_update_entry(entry, data=entry_data) await self.hass.config_entries.async_reload(entry.entry_id) @@ -497,6 +505,7 @@ async def async_step_customizing(self, user_input=None): if entities: schema.update({ vol.Required('entity'): vol.In(entities), + vol.Optional('yaml_mode', default=user_input.get('yaml_mode', False)): cv.boolean, }) else: tip = f'None entities in `{domain}`' @@ -545,6 +554,7 @@ async def async_step_customizing(self, user_input=None): }) schema.update({ vol.Optional('model_specified'): str, + vol.Optional('yaml_mode', default=user_input.get('yaml_mode', False)): cv.boolean, }) if last_step := self.context.get('last_step', last_step): @@ -556,22 +566,28 @@ async def async_step_customizing(self, user_input=None): if not options: tip += f'\n\n无可用的自定义选项。' if in_china(self.hass) else f'\n\nNo customizable options are available.' - if 'bool2selects' in options: - options['bool2selects'] = cv.multi_select(dict(zip(bool2selects, bool2selects))) - customizes['bool2selects'] = [ - k - for k in bool2selects - if customizes.get(k) - ] - schema.update({ - vol.Optional(k, default=customizes.get(k, vol.UNDEFINED), description=k): v - for k, v in options.items() - }) - schema.update({ - vol.Optional('reset_customizes', default=False): cv.boolean, - }) - customizes.pop('bool2selects', None) customizes.pop('extend_miot_specs', None) + self.context['yaml_mode'] = user_input.get('yaml_mode') + if self.context['yaml_mode']: + schema.update({ + vol.Optional('yaml_customizes', default=customizes): ObjectSelector(), + }) + else: + if 'bool2selects' in options: + options['bool2selects'] = cv.multi_select(dict(zip(bool2selects, bool2selects))) + customizes['bool2selects'] = [ + k + for k in bool2selects + if customizes.get(k) + ] + schema.update({ + vol.Optional(k, default=customizes.get(k, vol.UNDEFINED), description=k): v + for k, v in options.items() + }) + schema.update({ + vol.Optional('reset_customizes', default=False): cv.boolean, + }) + customizes.pop('bool2selects', None) if customizes: tip += f'\n```yaml\n{yaml.dump(customizes)}\n```' diff --git a/custom_components/xiaomi_miot/translations/en.json b/custom_components/xiaomi_miot/translations/en.json index 59e495909d..90c2380d56 100644 --- a/custom_components/xiaomi_miot/translations/en.json +++ b/custom_components/xiaomi_miot/translations/en.json @@ -55,6 +55,8 @@ "model": "Select device model", "model_specified": "Specified device model (Optional)", "only_main_entity": "Only main (parent) entities", + "yaml_mode": "YAML Mode", + "yaml_customizes": "Customizes YAML Code", "reset_customizes": "Reset to default customizes", "binary_sensor_attributes": "binary_sensor_attributes", diff --git a/custom_components/xiaomi_miot/translations/zh-Hans.json b/custom_components/xiaomi_miot/translations/zh-Hans.json index dd858ceb79..9b3d35c2e7 100644 --- a/custom_components/xiaomi_miot/translations/zh-Hans.json +++ b/custom_components/xiaomi_miot/translations/zh-Hans.json @@ -55,6 +55,8 @@ "model": "选择设备型号", "model_specified": "指定设备型号 (可选)", "only_main_entity": "仅主(父)实体", + "yaml_mode": "YAML模式", + "yaml_customizes": "自定义选项代码", "reset_customizes": "重置为默认自定义", "bool2selects": "自定义选项" } diff --git a/custom_components/xiaomi_miot/translations/zh-Hant.json b/custom_components/xiaomi_miot/translations/zh-Hant.json index 225f43f218..5ed1918baf 100644 --- a/custom_components/xiaomi_miot/translations/zh-Hant.json +++ b/custom_components/xiaomi_miot/translations/zh-Hant.json @@ -55,6 +55,8 @@ "model": "選擇裝置型號", "model_specified": "指定裝置型號 (可選)", "only_main_entity": "僅主(父)實體", + "yaml_mode": "YAML模式", + "yaml_customizes": "自訂選項代碼", "reset_customizes": "重設為預設自訂", "bool2selects": "自訂選項" }