92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
# Payload: {
|
|
# 'trigger_type': 'event',
|
|
# 'event_type': 'homematic.keypress',
|
|
# 'context': <homeassistant.core.Context object at 0x7f8c9eebe890>,
|
|
# 'name': '000B5D898D535E',
|
|
# 'param': 'PRESS_SHORT',
|
|
# 'channel': 3
|
|
# }
|
|
|
|
|
|
TASTER_ID_JOEL = "000B5D898D535E" # Replace with your actual Taster ID
|
|
ROLLO_ID_JOEL = "cover.00115a498e0439" # Replace with your actual Rollo ID
|
|
POSITION_UP = 66
|
|
POSITION_DOWN = 20
|
|
|
|
last_action = dict()
|
|
|
|
state.persist('pyscript.last_action')
|
|
|
|
""" The state of the short-presses will be stored in the pyscript.last_action variable."""
|
|
""" In the pattern of {key}_{0|1} - 0 = not pressed, 1 = pressed """
|
|
def toggle_keystate(key):
|
|
"""Toggle keypress state."""
|
|
prefixed_key = f'key_{key}'
|
|
|
|
if prefixed_key in last_action:
|
|
last_action[prefixed_key] = 1 if last_action.get(prefixed_key) == 0 else 0
|
|
else:
|
|
last_action[prefixed_key] = 1
|
|
|
|
log.info(f"Key {key} toggled to {last_action[prefixed_key]}")
|
|
log.info(f"Key dict {last_action}")
|
|
|
|
|
|
def get_keystate(key):
|
|
prefixed_key = f'key_{key}'
|
|
|
|
return last_action.get(prefixed_key, 0)
|
|
|
|
|
|
@event_trigger('homematic.keypress')
|
|
def taster(**payload):
|
|
if payload.get("name") != TASTER_ID_JOEL:
|
|
return
|
|
|
|
param = payload.get("param")
|
|
if param == "PRESS_SHORT":
|
|
# Check if the event is a short press
|
|
log.info("Short press detected")
|
|
|
|
channel_no = payload.get("channel")
|
|
|
|
""" Offen """
|
|
if channel_no == 5:
|
|
# Perform action for channel 5
|
|
if get_keystate("5") == 1:
|
|
cover.open_cover(entity_id=ROLLO_ID_JOEL)
|
|
else:
|
|
cover.stop_cover(entity_id=ROLLO_ID_JOEL)
|
|
|
|
toggle_keystate("5")
|
|
elif channel_no == 6:
|
|
# Perform action for channel 6
|
|
if get_keystate("6") == 1:
|
|
cover.close_cover(entity_id=ROLLO_ID_JOEL)
|
|
else:
|
|
cover.stop_cover(entity_id=ROLLO_ID_JOEL)
|
|
|
|
toggle_keystate("6")
|
|
|
|
elif channel_no == 3:
|
|
# Perform action for channel 3
|
|
log.info("Channel 3 action")
|
|
# Add your action here, e.g., toggle a light
|
|
|
|
if param == "PRESS_LONG":
|
|
# Check if the event is a long press
|
|
log.info("Long press detected")
|
|
|
|
channel_no = payload.get("channel")
|
|
|
|
if channel_no == 5:
|
|
log.info("Channel 5 action")
|
|
cover.set_cover_position(entity_id=ROLLO_ID_JOEL, position=POSITION_UP)
|
|
elif channel_no == 6:
|
|
log.info("Channel 6 action")
|
|
cover.set_cover_position(entity_id=ROLLO_ID_JOEL, position=POSITION_DOWN)
|
|
|
|
|
|
log.info(f"taster: {payload}")
|
|
|
|
|