From 0b068649ca5542484b978ff431902fd362e8460e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sun, 22 Nov 2020 10:23:27 +0000 Subject: [PATCH] Add identify device function. --- .gitignore | 2 ++ holiday.py | 4 +--- neohub.py | 44 ++++++++++---------------------------------- neostat.py | 15 +++++++++++++-- test.py | 1 + 5 files changed, 27 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 87d3fc3..e7a4e11 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ __pycache__ *~ +.kdev4 +*.kdev4 diff --git a/holiday.py b/holiday.py index df8c91f..108ec0b 100644 --- a/holiday.py +++ b/holiday.py @@ -6,6 +6,7 @@ import datetime import enum + class Holiday: """ Class representing Holidays @@ -29,19 +30,16 @@ class Holiday: self._ids = reply["ids"] - @property def start(self): """ Beginning of holiday. """ return self._start - @property def end(self): """ End of holiday. """ return self._end - @property def ids(self): """ Devices that have holiday set up. """ diff --git a/neohub.py b/neohub.py index dbc89ce..1c82705 100644 --- a/neohub.py +++ b/neohub.py @@ -17,13 +17,11 @@ class NeoHub: self._logger = logging.getLogger('neohub') pass - async def connect(self, host='Neo-Hub', port='4242'): self._host = host 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) encoded_message = bytearray(json.dumps(message) + "\0\r", "utf-8") self._logger.debug(f"Sending message: {encoded_message}") @@ -40,7 +38,7 @@ class NeoHub: try: reply = json.loads(json_string) - except json.decoder.JSONDecodeError: + except json.decoder.JSONDecodeError as e: if expected_reply is None: raise(e) else: @@ -55,7 +53,6 @@ class NeoHub: self._logger.error(f"Unexpected reply: {reply}") return False - async def firmware(self): """ NeoHub firmware version @@ -67,8 +64,6 @@ class NeoHub: firmware_version = int(result['firmware version']) return firmware_version - - async def get_system(self): """ Get system wide variables @@ -81,7 +76,6 @@ class NeoHub: system_data = System(data) return system_data - async def reset(self): """ Reboot neohub @@ -100,7 +94,6 @@ class NeoHub: else: return False - async def set_channel(self, channel: int): """ Set ZigBee channel. @@ -114,7 +107,6 @@ class NeoHub: result = await self._send(message, reply) return result - async def set_temp_format(self, temp_format: str): """ Set temperature format to C or F @@ -126,7 +118,6 @@ class NeoHub: result = await self._send(message, reply) return result - async def set_format(self, format: ScheduleFormat): """ Sets schedule format @@ -140,15 +131,14 @@ class NeoHub: result = await self._send(message, reply) return result - async def set_away(self, state: bool): """ Enables away mode for all devices. 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} @@ -157,7 +147,6 @@ class NeoHub: result = await self._send(message, reply) return result - async def holiday(self, start: datetime.datetime, end: datetime.datetime): """ Sets holiday mode. @@ -171,7 +160,6 @@ class NeoHub: result = await self._send(message) return result - async def get_holiday(self): """ Get list of holidays @@ -183,7 +171,6 @@ class NeoHub: result = await self._send(message) return Holiday(result) - async def cancel_holiday(self): """ Cancels holidays and returns to normal schedule @@ -195,7 +182,6 @@ class NeoHub: result = await self._send(message, reply) return result - async def get_zones(self): """ Get list of all thermostats @@ -207,12 +193,11 @@ class NeoHub: zones = await self._send(message) result = [] - for name,zone_id in zones.items(): - result.append(NeoStat(name, zone_id)) + for name, zone_id in zones.items(): + result.append(NeoStat(self, name, zone_id)) return result - async def get_devices(self): """ Returns list of devices @@ -225,7 +210,6 @@ class NeoHub: result = await self._send(message) return result - async def get_device_list(self, zone: str): """ Returns list of devices associated with zone @@ -239,7 +223,6 @@ class NeoHub: else: return result[zone] - async def devices_sn(self): """ Returns serial numbers of attached devices @@ -252,7 +235,6 @@ class NeoHub: result = await self._send(message) return result - async def set_ntp(self, state: bool): """ Enables NTP client on Neohub @@ -264,8 +246,7 @@ class NeoHub: result = await self._send(message, reply) return result - - async def set_date(self, date = None): + async def set_date(self, date=None): """ Sets current date @@ -281,8 +262,7 @@ class NeoHub: result = await self._send(message, reply) return result - - async def set_time(self, time = None): + async def set_time(self, time=None): """ Sets current time @@ -298,8 +278,7 @@ class NeoHub: result = await self._send(message, reply) 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 """ @@ -309,7 +288,6 @@ class NeoHub: result = await self.set_time(date_time) return result - async def manual_dst(self, state: bool): """ Manually enables/disables daylight saving time @@ -321,8 +299,7 @@ class NeoHub: result = await self._send(message, reply) 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. @@ -340,7 +317,6 @@ class NeoHub: result = await self._send(message, reply) return result - async def identify(self): """ Flashes red LED light diff --git a/neostat.py b/neostat.py index 206f22d..f61282d 100644 --- a/neostat.py +++ b/neostat.py @@ -9,7 +9,8 @@ class NeoStat: 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._zone_id = zone_id @@ -18,8 +19,18 @@ class NeoStat: """ Zone name. """ return self._name - @property def zone_id(self): """ End of holiday. """ 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 diff --git a/test.py b/test.py index a9a90d5..f7348d0 100755 --- a/test.py +++ b/test.py @@ -11,6 +11,7 @@ import neohub from enums import ScheduleFormat + async def run(): hub = neohub.NeoHub() await hub.connect()