Skip to content

Commit

Permalink
📐 add yaml mode for customizing
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Feb 19, 2025
1 parent fac5ae2 commit 762ddce
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
64 changes: 40 additions & 24 deletions custom_components/xiaomi_miot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}`'
Expand Down Expand Up @@ -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):
Expand All @@ -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```'

Expand Down
2 changes: 2 additions & 0 deletions custom_components/xiaomi_miot/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions custom_components/xiaomi_miot/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"model": "选择设备型号",
"model_specified": "指定设备型号 (可选)",
"only_main_entity": "仅主(父)实体",
"yaml_mode": "YAML模式",
"yaml_customizes": "自定义选项代码",
"reset_customizes": "重置为默认自定义",
"bool2selects": "自定义选项"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/xiaomi_miot/translations/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"model": "選擇裝置型號",
"model_specified": "指定裝置型號 (可選)",
"only_main_entity": "僅主(父)實體",
"yaml_mode": "YAML模式",
"yaml_customizes": "自訂選項代碼",
"reset_customizes": "重設為預設自訂",
"bool2selects": "自訂選項"
}
Expand Down

0 comments on commit 762ddce

Please sign in to comment.