Skip to content

Commit 1b0a45c

Browse files
committed
Added code to wait for server connection
1 parent 721c993 commit 1b0a45c

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

MacroPad_RPC_Home_Assistant/code.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MqttError(Exception):
4848
def rpc_call(function, *args, **kwargs):
4949
response = rpc.call(function, *args, **kwargs)
5050
if response["error"]:
51-
if response["error_type"]:
51+
if response["error_type"] == "mqtt":
5252
raise MqttError(response["message"])
5353
raise RpcError(response["message"])
5454
return response["return_val"]
@@ -64,6 +64,15 @@ def update_key(key_number):
6464
else:
6565
macropad.pixels[key_number] = 0
6666

67+
server_is_running = False
68+
print("Waiting for server...")
69+
while not server_is_running:
70+
try:
71+
server_is_running = rpc_call("is_running")
72+
print("Connected")
73+
except RpcError:
74+
pass
75+
6776
mqtt_init()
6877
last_macropad_encoder_value = macropad.encoder
6978

MacroPad_RPC_Home_Assistant/rpc_ha_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ def mqtt_get_last_value(topic):
6868
return last_mqtt_messages[topic]
6969
return None
7070

71+
def is_running():
72+
return True
73+
7174
def handle_rpc(packet):
7275
"""This function will verify good data in packet,
7376
call the method with parameters, and generate a response
7477
packet as the return value"""
78+
print("Received packet")
7579
func_name = packet['function']
7680
if func_name in PROTECTED_FUNCTIONS:
7781
return rpc.create_response_packet(error=True, message=f"{func_name}'() is a protected function and can not be called.")

0 commit comments

Comments
 (0)