forked from andrius/neohubapi
Add mechanism to compare expected replies.
This commit is contained in:
parent
1c75067de9
commit
135c6033bc
25
neohub.py
25
neohub.py
|
@ -18,7 +18,7 @@ class NeoHub:
|
||||||
self._port = port
|
self._port = port
|
||||||
|
|
||||||
|
|
||||||
async def _send(self, message):
|
async def _send(self, message, expected_reply = None):
|
||||||
reader, writer = await asyncio.open_connection(self._host, self._port)
|
reader, writer = await asyncio.open_connection(self._host, self._port)
|
||||||
encoded_message = bytearray(json.dumps(message) + "\0\r", "utf-8")
|
encoded_message = bytearray(json.dumps(message) + "\0\r", "utf-8")
|
||||||
self._logger.debug(f"Sending message: {encoded_message}")
|
self._logger.debug(f"Sending message: {encoded_message}")
|
||||||
|
@ -33,13 +33,23 @@ class NeoHub:
|
||||||
writer.close()
|
writer.close()
|
||||||
await writer.wait_closed()
|
await writer.wait_closed()
|
||||||
|
|
||||||
return json.loads(json_string)
|
reply = json.loads(json_string)
|
||||||
|
|
||||||
|
if expected_reply is None:
|
||||||
|
return reply
|
||||||
|
else:
|
||||||
|
if reply == expected_reply:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._logger.error(f"Unexpected reply: {reply}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def firmware(self):
|
async def firmware(self):
|
||||||
"""
|
"""
|
||||||
NeoHub firmware version
|
NeoHub firmware version
|
||||||
"""
|
"""
|
||||||
|
|
||||||
message = {"FIRMWARE": 0}
|
message = {"FIRMWARE": 0}
|
||||||
|
|
||||||
result = await self._send(message)
|
result = await self._send(message)
|
||||||
|
@ -67,16 +77,19 @@ class NeoHub:
|
||||||
|
|
||||||
Returns True if Restart is initiated
|
Returns True if Restart is initiated
|
||||||
"""
|
"""
|
||||||
|
|
||||||
message = {"RESET": 0}
|
message = {"RESET": 0}
|
||||||
|
reply = {"Restarting": 1}
|
||||||
|
|
||||||
firmware_version = await self.firmware()
|
firmware_version = await self.firmware()
|
||||||
result = ""
|
result = ""
|
||||||
if firmware_version >= 2027:
|
if firmware_version >= 2027:
|
||||||
result = await self._send(message)
|
result = await self._send(message, reply)
|
||||||
return result['Restarting'] == 1
|
return result
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def set_channel(self, channel: int):
|
async def set_channel(self, channel: int):
|
||||||
"""
|
"""
|
||||||
Set ZigBee channel.
|
Set ZigBee channel.
|
||||||
|
@ -85,5 +98,7 @@ class NeoHub:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
message = {"SET_CHANNEL": channel}
|
message = {"SET_CHANNEL": channel}
|
||||||
|
reply = {"result":"Trying to change channel"}
|
||||||
|
|
||||||
result = await self._send(message)
|
result = await self._send(message, reply)
|
||||||
|
return result
|
||||||
|
|
4
test.py
4
test.py
|
@ -14,9 +14,9 @@ async def run():
|
||||||
await hub.connect()
|
await hub.connect()
|
||||||
firmware_version = await hub.firmware()
|
firmware_version = await hub.firmware()
|
||||||
print(firmware_version)
|
print(firmware_version)
|
||||||
result = await hub.set_channel(14)
|
result = await hub.set_channel(11)
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.ERROR)
|
||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|
Loading…
Reference in New Issue