diff --git a/enums.py b/enums.py index 6ad189b..e96564f 100644 --- a/enums.py +++ b/enums.py @@ -20,3 +20,15 @@ class ScheduleFormat(str, enum.Enum): ONE = "24HOURSFIXED" TWO = "5DAY/2DAY" SEVEN = "7DAY" + +def schedule_format_int_to_enum(int_format): + if int_format == 0: + return ScheduleFormat.ZERO + elif int_format == 1: + return ScheduleFormat.ONE + elif int_format == 2: + return ScheduleFormat.TWO + elif int_format == 4: + return ScheduleFormat.SEVEN + else: + raise ValueError('Unrecognized ScheduleFormat') diff --git a/holiday.py b/holiday.py deleted file mode 100644 index 108ec0b..0000000 --- a/holiday.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 - -# SPDX-FileCopyrightText: 2020 Andrius Štikonas -# SPDX-License-Identifier: LGPL-3.0-or-later - -import datetime -import enum - - -class Holiday: - """ - Class representing Holidays - - start, end: holiday datetime objects or None - ids - """ - - def __init__(self, reply: str): - start = reply["start"] - if start: - self._start = datetime.datetime.strptime(start.strip(), "%a %b %d %H:%M:%S %Y") - else: - self._start = None - - end = reply["end"] - if end: - self.end = datetime.datetime.strptime(end.strip(), "%a %b %d %H:%M:%S %Y") - else: - self._end = None - - 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. """ - return self._ids diff --git a/neohub.py b/neohub.py index 7427849..1ab74b3 100644 --- a/neohub.py +++ b/neohub.py @@ -5,9 +5,10 @@ import asyncio import datetime import json import logging +from types import SimpleNamespace from enums import ScheduleFormat -from system import System +from enums import schedule_format_int_to_enum from holiday import Holiday from neostat import NeoStat @@ -37,7 +38,7 @@ class NeoHub: await writer.wait_closed() try: - reply = json.loads(json_string) + reply = json.loads(json_string, object_hook=lambda d: SimpleNamespace(**d)) except json.decoder.JSONDecodeError as e: if expected_reply is None: raise(e) @@ -67,14 +68,13 @@ class NeoHub: async def get_system(self): """ Get system wide variables - - Returns System object """ message = {"GET_SYSTEM": 0} data = await self._send(message) - system_data = System(data) - return system_data + data.FORMAT = schedule_format_int_to_enum(data.FORMAT) + data.ALT_TIMER_FORMAT = schedule_format_int_to_enum(data.ALT_TIMER_FORMAT) + return data async def reset(self): """ @@ -164,12 +164,14 @@ class NeoHub: """ Get list of holidays - Returns Holiday object + start end end times are converted to datetimes """ message = {"GET_HOLIDAY": 0} result = await self._send(message) - return Holiday(result) + result.start = datetime.datetime.strptime(result.start.strip(), "%a %b %d %H:%M:%S %Y") if result.start else None + result.end = datetime.datetime.strptime(result.end.strip(), "%a %b %d %H:%M:%S %Y") if result.end else None + return result async def cancel_holiday(self): """ @@ -193,7 +195,7 @@ class NeoHub: zones = await self._send(message) result = [] - for name, zone_id in zones.items(): + for name, zone_id in zones.__dict__.items(): result.append(NeoStat(self, name, zone_id)) return result diff --git a/system.py b/system.py deleted file mode 100644 index eee146e..0000000 --- a/system.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -# SPDX-FileCopyrightText: 2020 Andrius Štikonas -# SPDX-License-Identifier: LGPL-3.0-or-later - -from enums import ScheduleFormat - - -def schedule_format(int_format): - if int_format == 0: - return ScheduleFormat.ZERO - elif int_format == 1: - return ScheduleFormat.ONE - elif int_format == 2: - return ScheduleFormat.TWO - elif int_format == 4: - return ScheduleFormat.SEVEN - else: - raise ValueError('Unrecognized ScheduleFormat') - - -class System: - def __init__(self, system_info): - self.dst_auto = system_info['DST_AUTO'] - self.dst_on = system_info['DST_ON'] - self.timer_format = schedule_format(system_info['FORMAT']) - # If system timer format is set to non programmable, then any time clock remain - # in the previous setting which is stored in ALT_TIMER_FORMAT. - self.alt_timer_format = schedule_format(system_info['ALT_TIMER_FORMAT']) - self.ntp = system_info['NTP_ON'] == "Running" - self.hub_type = system_info['HUB_TYPE'] - self.hub_version = system_info['HUB_VERSION'] - self.temperature_unit = system_info["CORF"] - self.timezone = system_info['TIME_ZONE'] - self.time = system_info['UTC']