Skip to content

Commit

Permalink
Adding the option to choose between TCP and Websocket transport protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltan-fedor committed Apr 21, 2022
1 parent 767abc9 commit e198715
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def main():
mqtt = MQTTClient(
access_token=yolink_service.access_token.token,
client_id=str(time.time()), # which just need a unique client id
home_id=home_id
home_id=home_id,
transport='websockets'
)

mqtt.client.subscribe(f"yl-home/{home_id}/+/report", qos=0)
Expand Down
13 changes: 10 additions & 3 deletions mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class MQTTClient:
See https://pypi.org/project/paho-mqtt
"""

def __init__(self, access_token: str, client_id: str, home_id: str):
def __init__(self, access_token: str, client_id: str, home_id: str, transport: str = 'tcp'):
"""
:param access_token:
:param client_id:
:param home_id: HomeId retrieved via the HTTP API
:param transport: 'tcp' or 'websockets'
"""

# this is required to subscribe to events coming from the devices of the given home
self.home_id = home_id
Expand All @@ -18,14 +25,14 @@ def __init__(self, access_token: str, client_id: str, home_id: str):
clean_session=True,
userdata=None,
protocol=mqtt.MQTTv311,
transport='tcp' # websocket or tcp
transport=transport # websocket or tcp
)
self.client.username_pw_set(username=access_token)
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_log = self.on_log
self.client.connect(host="api.yosmart.com",
port=8003,
port=8003 if transport == 'tcp' else 8004,
keepalive=60)

def on_connect(self, client, userdata, flags, rc):
Expand Down

0 comments on commit e198715

Please sign in to comment.