Skip to content

Commit

Permalink
Add support button device trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Nov 24, 2020
1 parent 62f2ffc commit 8eab5de
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
13 changes: 12 additions & 1 deletion custom_components/xiaomi_gateway3/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]
Expand Down Expand Up @@ -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 = '<a href="%s" target="_blank">Open Log<a>'
HTML = (f'<!DOCTYPE html><html><head><title>{TITLE}</title>'
Expand Down
67 changes: 67 additions & 0 deletions custom_components/xiaomi_gateway3/device_trigger.py
Original file line number Diff line number Diff line change
@@ -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()),
})
}
19 changes: 17 additions & 2 deletions custom_components/xiaomi_gateway3/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand All @@ -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"
}
}
}

0 comments on commit 8eab5de

Please sign in to comment.