|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +""" |
| 5 | +Home Assistant Remote Procedure Call for MacroPad. |
| 6 | +""" |
| 7 | +import time |
| 8 | +import displayio |
| 9 | +import terminalio |
| 10 | +from adafruit_display_shapes.rect import Rect |
| 11 | +from rpc import RpcClient, RpcError |
| 12 | +from adafruit_display_text import label |
| 13 | +from adafruit_macropad import MacroPad |
| 14 | +from secrets import secrets |
| 15 | + |
| 16 | +macropad = MacroPad() |
| 17 | +rpc = RpcClient() |
| 18 | + |
| 19 | +COMMAND_TOPIC = "macropad/peripheral" |
| 20 | +SUBSCRIBE_TOPICS = ("stat/demoswitch/POWER", "stat/office-light/POWER") |
| 21 | +ENCODER_ITEM = 0 |
| 22 | +KEY_LABELS = ("Demo", "Office") |
| 23 | +UPDATE_DELAY = 0.25 |
| 24 | +NEOPIXEL_COLORS = { |
| 25 | + "OFF": 0xff0000, |
| 26 | + "ON": 0x00ff00, |
| 27 | +} |
| 28 | + |
| 29 | +class MqttError(Exception): |
| 30 | + """For MQTT Specific Errors""" |
| 31 | + pass |
| 32 | +# Set up displayio group with all the labels |
| 33 | +group = displayio.Group() |
| 34 | +for key_index in range(12): |
| 35 | + x = key_index % 3 |
| 36 | + y = key_index // 3 |
| 37 | + group.append(label.Label(terminalio.FONT, text=(str(KEY_LABELS[key_index]) if key_index < len(KEY_LABELS) else ''), color=0xFFFFFF, |
| 38 | + anchored_position=((macropad.display.width - 1) * x / 2, |
| 39 | + macropad.display.height - 1 - |
| 40 | + (3 - y) * 12), |
| 41 | + anchor_point=(x / 2, 1.0))) |
| 42 | +group.append(Rect(0, 0, macropad.display.width, 12, fill=0xFFFFFF)) |
| 43 | +group.append(label.Label(terminalio.FONT, text='Home Assistant', color=0x000000, |
| 44 | + anchored_position=(macropad.display.width//2, -2), |
| 45 | + anchor_point=(0.5, 0.0))) |
| 46 | +macropad.display.show(group) |
| 47 | + |
| 48 | +def rpc_call(function, *args, **kwargs): |
| 49 | + response = rpc.call(function, *args, **kwargs) |
| 50 | + if response["error"]: |
| 51 | + if response["error_type"]: |
| 52 | + raise MqttError(response["message"]) |
| 53 | + raise RpcError(response["message"]) |
| 54 | + return response["return_val"] |
| 55 | + |
| 56 | +def mqtt_init(): |
| 57 | + rpc_call("mqtt_init", secrets["mqtt_broker"], username=secrets["mqtt_username"], password=secrets["mqtt_password"], port=secrets["mqtt_port"]) |
| 58 | + rpc_call("mqtt_connect") |
| 59 | + |
| 60 | +def update_key(key_number): |
| 61 | + switch_state = rpc_call("mqtt_get_last_value", SUBSCRIBE_TOPICS[key_number]) |
| 62 | + if switch_state is not None: |
| 63 | + macropad.pixels[key_number] = NEOPIXEL_COLORS[switch_state] |
| 64 | + else: |
| 65 | + macropad.pixels[key_number] = None |
| 66 | + |
| 67 | +mqtt_init() |
| 68 | +last_macropad_encoder_value = macropad.encoder |
| 69 | + |
| 70 | +for key_number, topic in enumerate(SUBSCRIBE_TOPICS): |
| 71 | + rpc_call("mqtt_subscribe", topic) |
| 72 | + update_key(key_number) |
| 73 | + |
| 74 | +while True: |
| 75 | + output = {} |
| 76 | + |
| 77 | + key_event = macropad.keys.events.get() |
| 78 | + if key_event and key_event.pressed: |
| 79 | + output["key_number"] = key_event.key_number |
| 80 | + |
| 81 | + if macropad.encoder != last_macropad_encoder_value: |
| 82 | + output["encoder"] = macropad.encoder - last_macropad_encoder_value |
| 83 | + last_macropad_encoder_value = macropad.encoder |
| 84 | + |
| 85 | + macropad.encoder_switch_debounced.update() |
| 86 | + if macropad.encoder_switch_debounced.pressed and "key_number" not in output and ENCODER_ITEM is not None: |
| 87 | + output["key_number"] = ENCODER_ITEM |
| 88 | + |
| 89 | + if output: |
| 90 | + try: |
| 91 | + rpc_call("mqtt_publish", COMMAND_TOPIC, output) |
| 92 | + if "key_number" in output: |
| 93 | + time.sleep(UPDATE_DELAY) |
| 94 | + update_key(output["key_number"]) |
| 95 | + except MqttError: |
| 96 | + mqtt_init() |
| 97 | + except RpcError as err_msg: |
| 98 | + print(err_msg) |
0 commit comments