Add identify device function.

This commit is contained in:
Andrius Štikonas 2020-11-22 10:23:27 +00:00
parent 7511b7b29e
commit 0b068649ca
5 changed files with 27 additions and 39 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
__pycache__ __pycache__
*~ *~
.kdev4
*.kdev4

View File

@ -6,6 +6,7 @@
import datetime import datetime
import enum import enum
class Holiday: class Holiday:
""" """
Class representing Holidays Class representing Holidays
@ -29,19 +30,16 @@ class Holiday:
self._ids = reply["ids"] self._ids = reply["ids"]
@property @property
def start(self): def start(self):
""" Beginning of holiday. """ """ Beginning of holiday. """
return self._start return self._start
@property @property
def end(self): def end(self):
""" End of holiday. """ """ End of holiday. """
return self._end return self._end
@property @property
def ids(self): def ids(self):
""" Devices that have holiday set up. """ """ Devices that have holiday set up. """

View File

@ -17,12 +17,10 @@ class NeoHub:
self._logger = logging.getLogger('neohub') self._logger = logging.getLogger('neohub')
pass pass
async def connect(self, host='Neo-Hub', port='4242'): async def connect(self, host='Neo-Hub', port='4242'):
self._host = host self._host = host
self._port = port self._port = port
async def _send(self, message, expected_reply=None): 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")
@ -40,7 +38,7 @@ class NeoHub:
try: try:
reply = json.loads(json_string) reply = json.loads(json_string)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError as e:
if expected_reply is None: if expected_reply is None:
raise(e) raise(e)
else: else:
@ -55,7 +53,6 @@ class NeoHub:
self._logger.error(f"Unexpected reply: {reply}") self._logger.error(f"Unexpected reply: {reply}")
return False return False
async def firmware(self): async def firmware(self):
""" """
NeoHub firmware version NeoHub firmware version
@ -67,8 +64,6 @@ class NeoHub:
firmware_version = int(result['firmware version']) firmware_version = int(result['firmware version'])
return firmware_version return firmware_version
async def get_system(self): async def get_system(self):
""" """
Get system wide variables Get system wide variables
@ -81,7 +76,6 @@ class NeoHub:
system_data = System(data) system_data = System(data)
return system_data return system_data
async def reset(self): async def reset(self):
""" """
Reboot neohub Reboot neohub
@ -100,7 +94,6 @@ class NeoHub:
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.
@ -114,7 +107,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_temp_format(self, temp_format: str): async def set_temp_format(self, temp_format: str):
""" """
Set temperature format to C or F Set temperature format to C or F
@ -126,7 +118,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_format(self, format: ScheduleFormat): async def set_format(self, format: ScheduleFormat):
""" """
Sets schedule format Sets schedule format
@ -140,15 +131,14 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_away(self, state: bool): async def set_away(self, state: bool):
""" """
Enables away mode for all devices. Enables away mode for all devices.
Puts thermostats into frost mode and timeclocks are set to off. Puts thermostats into frost mode and timeclocks are set to off.
Instead of this function it is recommended to use frost on/off commands. Instead of this function it is recommended to use frost on/off commands
List of devices affected by this can be restricted using GLOBAL_DEV_LIST command List of affected devices can be restricted using GLOBAL_DEV_LIST command
""" """
message = {"AWAY_ON" if state else "AWAY_OFF": 0} message = {"AWAY_ON" if state else "AWAY_OFF": 0}
@ -157,7 +147,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def holiday(self, start: datetime.datetime, end: datetime.datetime): async def holiday(self, start: datetime.datetime, end: datetime.datetime):
""" """
Sets holiday mode. Sets holiday mode.
@ -171,7 +160,6 @@ class NeoHub:
result = await self._send(message) result = await self._send(message)
return result return result
async def get_holiday(self): async def get_holiday(self):
""" """
Get list of holidays Get list of holidays
@ -183,7 +171,6 @@ class NeoHub:
result = await self._send(message) result = await self._send(message)
return Holiday(result) return Holiday(result)
async def cancel_holiday(self): async def cancel_holiday(self):
""" """
Cancels holidays and returns to normal schedule Cancels holidays and returns to normal schedule
@ -195,7 +182,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def get_zones(self): async def get_zones(self):
""" """
Get list of all thermostats Get list of all thermostats
@ -208,11 +194,10 @@ class NeoHub:
zones = await self._send(message) zones = await self._send(message)
result = [] result = []
for name, zone_id in zones.items(): for name, zone_id in zones.items():
result.append(NeoStat(name, zone_id)) result.append(NeoStat(self, name, zone_id))
return result return result
async def get_devices(self): async def get_devices(self):
""" """
Returns list of devices Returns list of devices
@ -225,7 +210,6 @@ class NeoHub:
result = await self._send(message) result = await self._send(message)
return result return result
async def get_device_list(self, zone: str): async def get_device_list(self, zone: str):
""" """
Returns list of devices associated with zone Returns list of devices associated with zone
@ -239,7 +223,6 @@ class NeoHub:
else: else:
return result[zone] return result[zone]
async def devices_sn(self): async def devices_sn(self):
""" """
Returns serial numbers of attached devices Returns serial numbers of attached devices
@ -252,7 +235,6 @@ class NeoHub:
result = await self._send(message) result = await self._send(message)
return result return result
async def set_ntp(self, state: bool): async def set_ntp(self, state: bool):
""" """
Enables NTP client on Neohub Enables NTP client on Neohub
@ -264,7 +246,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_date(self, date=None): async def set_date(self, date=None):
""" """
Sets current date Sets current date
@ -281,7 +262,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_time(self, time=None): async def set_time(self, time=None):
""" """
Sets current time Sets current time
@ -298,7 +278,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_datetime(self, date_time=None): async def set_datetime(self, date_time=None):
""" """
Convenience method to set both date and time Convenience method to set both date and time
@ -309,7 +288,6 @@ class NeoHub:
result = await self.set_time(date_time) result = await self.set_time(date_time)
return result return result
async def manual_dst(self, state: bool): async def manual_dst(self, state: bool):
""" """
Manually enables/disables daylight saving time Manually enables/disables daylight saving time
@ -321,7 +299,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def set_dst(self, state: bool, region=None): async def set_dst(self, state: bool, region=None):
""" """
Enables/disables automatic DST handling. Enables/disables automatic DST handling.
@ -340,7 +317,6 @@ class NeoHub:
result = await self._send(message, reply) result = await self._send(message, reply)
return result return result
async def identify(self): async def identify(self):
""" """
Flashes red LED light Flashes red LED light

View File

@ -9,7 +9,8 @@ class NeoStat:
Class representing NeoStat theormostat Class representing NeoStat theormostat
""" """
def __init__(self, name: str, zone_id: int): def __init__(self, hub, name: str, zone_id: int):
self._hub = hub
self._name = name self._name = name
self._zone_id = zone_id self._zone_id = zone_id
@ -18,8 +19,18 @@ class NeoStat:
""" Zone name. """ """ Zone name. """
return self._name return self._name
@property @property
def zone_id(self): def zone_id(self):
""" End of holiday. """ """ End of holiday. """
return self._zone_id return self._zone_id
async def identify(self):
"""
Flashes red LED light
"""
message = {"IDENTIFY_DEV": self.zone_id}
reply = {"result": "Device identifying"}
result = await self._hub._send(message, reply)
return result

View File

@ -11,6 +11,7 @@ import neohub
from enums import ScheduleFormat from enums import ScheduleFormat
async def run(): async def run():
hub = neohub.NeoHub() hub = neohub.NeoHub()
await hub.connect() await hub.connect()