diff --git a/custom_components/xiaomi_gateway3/core/utils.py b/custom_components/xiaomi_gateway3/core/utils.py index c5e925f1..6d0a85a5 100644 --- a/custom_components/xiaomi_gateway3/core/utils.py +++ b/custom_components/xiaomi_gateway3/core/utils.py @@ -206,9 +206,9 @@ ['13.2.85', None, 'button_2', None], ['13.3.85', None, 'button_3', None], ['13.4.85', None, 'button_4', None], - ['13.5.85', None, 'button_both', None], ['13.6.85', None, 'button_5', None], ['13.7.85', None, 'button_6', None], + ['13.5.85', None, 'button_both', None], [None, None, 'action', 'sensor'], ['8.0.2001', 'battery', 'battery', 'sensor'], ] @@ -430,6 +430,17 @@ def extract_jsons(raw) -> List[bytes]: return raw.replace(b'}{', b'}\n{').split(b'\n') +def get_buttons(model: str): + model, _ = model.split(' ', 1) + for device in DEVICES: + if model in device: + return [ + param[2] for param in device['params'] + if param[2].startswith('button') + ] + return None + + TITLE = "Xiaomi Gateway 3 Debug" NOTIFY_TEXT = 'Open Log' HTML = (f'{TITLE}' diff --git a/custom_components/xiaomi_gateway3/device_trigger.py b/custom_components/xiaomi_gateway3/device_trigger.py new file mode 100644 index 00000000..95b12346 --- /dev/null +++ b/custom_components/xiaomi_gateway3/device_trigger.py @@ -0,0 +1,67 @@ +import voluptuous as vol +from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA +from homeassistant.components.homeassistant.triggers import \ + state as state_trigger +from homeassistant.const import * +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.device_registry import DeviceEntry + +from . import DOMAIN +from .core import utils +from .sensor import BUTTON + +TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend( + { + vol.Required(CONF_TYPE): cv.string, + vol.Required('action'): cv.string + } +) + + +async def async_attach_trigger(hass, config, action, automation_info): + entity_registry = await hass.helpers.entity_registry.async_get_registry() + + device_id = config[CONF_DEVICE_ID] + entry = next( + entry for entry in entity_registry.entities.values() if + entry.device_id == device_id and entry.unique_id.endswith('action') + ) + + to_state = ( + config['action'] if config[CONF_TYPE] == 'button' else + f"{config[CONF_TYPE]}_{config['action']}" + ) + + state_config = { + CONF_PLATFORM: CONF_STATE, + CONF_ENTITY_ID: entry.entity_id, + state_trigger.CONF_TO: to_state + } + + state_config = state_trigger.TRIGGER_SCHEMA(state_config) + return await state_trigger.async_attach_trigger( + hass, state_config, action, automation_info, platform_type="device" + ) + + +async def async_get_triggers(hass, device_id): + device_registry = await hass.helpers.device_registry.async_get_registry() + device: DeviceEntry = device_registry.async_get(device_id) + buttons = utils.get_buttons(device.model) + if not buttons: + return None + + return [{ + CONF_PLATFORM: CONF_DEVICE, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_TYPE: button, + } for button in buttons] + + +async def async_get_trigger_capabilities(hass, config): + return { + "extra_fields": vol.Schema({ + vol.Required('action'): vol.In(BUTTON.values()), + }) + } diff --git a/custom_components/xiaomi_gateway3/translations/en.json b/custom_components/xiaomi_gateway3/translations/en.json index 4575dbb0..301b6b1c 100644 --- a/custom_components/xiaomi_gateway3/translations/en.json +++ b/custom_components/xiaomi_gateway3/translations/en.json @@ -21,12 +21,12 @@ } }, "token": { - "description": "[Obtain](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md) Mi Home token {error_text}", + "description": "[Obtain](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md) Mi Home token. Use **ZHA** mode ONLY if you [know what you doing](https://github.com/AlexxIT/XiaomiGateway3#zigbee-home-automation-mode) {error_text}", "data": { "host": "Host", "token": "Token", "ble": "Support BLE Devices", - "zha": "Zigbee Home Automation Mode" + "zha": "Zigbee Home Automation (ZHA) Mode" } } } @@ -41,5 +41,20 @@ "description": "{device_info}" } } + }, + "device_automation": { + "trigger_type": { + "button": "Button press", + "button_1": "1st button press", + "button_2": "2nd button press", + "button_3": "3rd button press", + "button_4": "4th button press", + "button_5": "5th button press", + "button_6": "6th button press", + "button_both": "Both button press", + "button_both_12": "1st and 2nd button press", + "button_both_13": "1st and 3rd button press", + "button_both_23": "2nd and 3rd button press" + } } } \ No newline at end of file