From 286b4ffe1a825f87eca8566bb16d355c1851f57c Mon Sep 17 00:00:00 2001 From: Nathan Merritt Date: Mon, 12 Sep 2022 11:33:33 -0600 Subject: [PATCH] fix imports, fix some and suppress remaining errors in Schedule.py --- BAC0/core/functions/Schedule.py | 58 ++++++++++++++------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/BAC0/core/functions/Schedule.py b/BAC0/core/functions/Schedule.py index 15b490b1..88f57502 100644 --- a/BAC0/core/functions/Schedule.py +++ b/BAC0/core/functions/Schedule.py @@ -4,37 +4,24 @@ # Copyright (C) 2015 by Christian Tremblay, P.Eng # Licensed under LGPLv3, see file LICENSE in this source tree. # -""" -ScheduleWrite.py - creation of ReinitializeDeviceRequest - -""" from ..io.Read import find_reason -from ..io.IOExceptions import ( - SegmentationNotSupported, - ReadPropertyException, - ReadPropertyMultipleException, - NoResponseFromController, - ApplicationNotStarted, -) +from ..io.IOExceptions import NoResponseFromController from ...core.utils.notes import note_and_log # --- standard Python modules --- -import datetime as dt +from datetime import time as dt_time +import typing as t # --- 3rd party modules --- -from bacpypes.pdu import Address, GlobalBroadcast -from bacpypes.primitivedata import Integer, Date, Time, CharacterString -from bacpypes.basetypes import DateTime +from bacpypes.pdu import Address +from bacpypes.primitivedata import Integer from bacpypes.apdu import WritePropertyRequest, SimpleAckPDU from bacpypes.iocb import IOCB from bacpypes.core import deferred -from bacpypes.basetypes import DateTime, DailySchedule, TimeValue, Time +from bacpypes.basetypes import DailySchedule, TimeValue from bacpypes.constructeddata import ArrayOf, Any -from bacpypes.primitivedata import Null, Atomic, Integer, Unsigned, Real, Enumerated - -from datetime import time as dt_time -from datetime import datetime as dt +from bacpypes.primitivedata import Real, Enumerated @note_and_log @@ -147,7 +134,7 @@ def send_weeklyschedule_request(self, request, timeout=10): if not isinstance(apdu, SimpleAckPDU): # expect an ACK self._log.warning("Not an ack, see debug for more infos.") self._log.debug( - "Not an ack. | APDU : {} / {}".format((apdu, type(apdu))) + "Not an ack. | APDU : {} / {}".format(apdu, type(apdu)) ) return if iocb.ioError: # unsuccessful: error/reject/abort @@ -205,8 +192,8 @@ def _read(prop): except Exception: raise () - schedule = {} - _state_text = None + schedule: t.Dict[str, t.Union[t.Dict, t.List]] = {} + _state_text: t.Union[str, range, t.List[str], None] = None offset_MV = 0 if len(object_references) == 0 else 1 try: @@ -239,29 +226,34 @@ def _read(prop): schedule["object_references"] = [] schedule["references_names"] = [] - schedule["states"] = {} + # re-ordered to make type inference possible, but not sure the logic here + # is 100% sound (ignored type warnings both look like actual problems). + # keeping logic intact and ignoring warnings for now + sched_states = {} if _state_text == ["inactive", "active"]: for i, each in enumerate(_state_text): - schedule["states"][each] = i + sched_states[each] = i presentValue = "{} ({})".format( - list(schedule["states"].keys())[int(presentValue.value)], + list(sched_states.keys())[int(presentValue.value)], presentValue.value, ) - elif _state_text != "analog": - for i, each in enumerate(_state_text): - schedule["states"][each] = i + offset_MV + elif _state_text == "analog": + sched_states = _state_text # type: ignore[assignment] + presentValue = "{}".format(presentValue.value) + else: try: + for i, each in enumerate(_state_text): # type: ignore[arg-type] + sched_states[each] = i + offset_MV presentValue = "{} ({})".format( - list(schedule["states"].keys())[ + list(sched_states.keys())[ int(presentValue.value) - offset_MV ], presentValue.value, ) except TypeError: presentValue = presentValue.value - else: - schedule["states"] = _state_text - presentValue = "{}".format(presentValue.value) + + schedule["states"] = sched_states schedule["reliability"] = reliability schedule["priority"] = priority schedule["presentValue"] = presentValue