diff --git a/src/aioapcaccess/__init__.py b/src/aioapcaccess/__init__.py index fc0db45..29b93fc 100644 --- a/src/aioapcaccess/__init__.py +++ b/src/aioapcaccess/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -from collections import OrderedDict _STATUS_CMD = b"\x00\x06status" _EOF = b"\x00\x00" @@ -29,7 +28,7 @@ } -async def request_status(host: str = "localhost", port: int = 3551) -> OrderedDict[str, str]: +async def request_status(host: str = "localhost", port: int = 3551) -> dict[str, str]: """Connect to the APCUPSd NIS and request its status and return the parsed dict. :param host: the host which apcupsd is listening on. @@ -40,14 +39,14 @@ async def request_status(host: str = "localhost", port: int = 3551) -> OrderedDi return parse_raw_status(await request_raw_status(host, port)) -def parse_raw_status(raw_status: bytes) -> OrderedDict[str, str]: +def parse_raw_status(raw_status: bytes) -> dict[str, str]: """Parse the raw status and return an ordered dict. :param raw_status: raw status string retrieved from apcupsd. :return: an ordered dict, where key is the field (e.g., "NOMINV") and value is the value. For example, {"NOMINV": "12.0 Volts", ..., "SERIALNO": "XXXXXXX"} """ - result = OrderedDict() + result = {} # Strip EOF from the status and decode to string. if not raw_status.endswith(_EOF): diff --git a/tests/__init__.py b/tests/__init__.py index 60f54ee..66ec433 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,7 +1,5 @@ """Sample raw status and its parsed dict.""" -from collections import OrderedDict - SAMPLE_STATUS = ( b"\x00" b"\x18APC : 001,051,1148\n\x00" @@ -60,59 +58,57 @@ b"\x00" ) -PARSED_DICT = OrderedDict( - [ - ("APC", "001,051,1148"), - ("DATE", "2016-02-09 21:13:11 +0000"), - ("HOSTNAME", "localhost"), - ("VERSION", "3.14.12 (29 March 2014) redhat"), - ("UPSNAME", "netrack"), - ("CABLE", "Custom Cable Smart"), - ("DRIVER", "APC Smart UPS (any)"), - ("UPSMODE", "Stand Alone"), - ("STARTTIME", "2016-02-09 16:06:47 +0000"), - ("MODEL", "SMART-UPS 1400"), - ("STATUS", "TRIM ONLINE"), - ("LINEV", "247.0 Volts"), - ("LOADPCT", "9.3 Percent Load Capacity"), - ("BCHARGE", "100.0 Percent"), - ("TIMELEFT", "128.0 Minutes"), - ("MBATTCHG", "5 Percent"), - ("MINTIMEL", "3 Minutes"), - ("MAXTIME", "0 Seconds"), - ("MAXLINEV", "250.9 Volts"), - ("MINLINEV", "247.0 Volts"), - ("OUTPUTV", "218.4 Volts"), - ("SENSE", "High"), - ("DWAKE", "0 Seconds"), - ("DSHUTD", "180 Seconds"), - ("DLOWBATT", "2 Minutes"), - ("LOTRANS", "196.0 Volts"), - ("HITRANS", "253.0 Volts"), - ("RETPCT", "15.0 Percent"), - ("ITEMP", "30.6 C Internal"), - ("ALARMDEL", "Low Battery"), - ("BATTV", "27.6 Volts"), - ("LINEFREQ", "50.0 Hz"), - ("LASTXFER", "High line voltage"), - ("NUMXFERS", "0"), - ("TONBATT", "0 Seconds"), - ("CUMONBATT", "0 Seconds"), - ("XOFFBATT", "N/A"), - ("SELFTEST", "NO"), - ("STESTI", "7 days"), - ("STATFLAG", "0x0500000A"), - ("DIPSW", "0x00"), - ("REG1", "0x00"), - ("REG2", "0x00"), - ("REG3", "0x00"), - ("MANDATE", "07/13/99"), - ("SERIALNO", "GS9229021308"), - ("BATTDATE", "13/11/15"), - ("NOMOUTV", "230 Volts"), - ("NOMBATTV", "24.0 Volts"), - ("EXTBATTS", "0"), - ("FIRMWARE", "70.11.I"), - ("END APC", "2016-02-09 21:13:26 +0000"), - ] -) +PARSED_DICT = { + "APC": "001,051,1148", + "DATE": "2016-02-09 21:13:11 +0000", + "HOSTNAME": "localhost", + "VERSION": "3.14.12 (29 March 2014) redhat", + "UPSNAME": "netrack", + "CABLE": "Custom Cable Smart", + "DRIVER": "APC Smart UPS (any)", + "UPSMODE": "Stand Alone", + "STARTTIME": "2016-02-09 16:06:47 +0000", + "MODEL": "SMART-UPS 1400", + "STATUS": "TRIM ONLINE", + "LINEV": "247.0 Volts", + "LOADPCT": "9.3 Percent Load Capacity", + "BCHARGE": "100.0 Percent", + "TIMELEFT": "128.0 Minutes", + "MBATTCHG": "5 Percent", + "MINTIMEL": "3 Minutes", + "MAXTIME": "0 Seconds", + "MAXLINEV": "250.9 Volts", + "MINLINEV": "247.0 Volts", + "OUTPUTV": "218.4 Volts", + "SENSE": "High", + "DWAKE": "0 Seconds", + "DSHUTD": "180 Seconds", + "DLOWBATT": "2 Minutes", + "LOTRANS": "196.0 Volts", + "HITRANS": "253.0 Volts", + "RETPCT": "15.0 Percent", + "ITEMP": "30.6 C Internal", + "ALARMDEL": "Low Battery", + "BATTV": "27.6 Volts", + "LINEFREQ": "50.0 Hz", + "LASTXFER": "High line voltage", + "NUMXFERS": "0", + "TONBATT": "0 Seconds", + "CUMONBATT": "0 Seconds", + "XOFFBATT": "N/A", + "SELFTEST": "NO", + "STESTI": "7 days", + "STATFLAG": "0x0500000A", + "DIPSW": "0x00", + "REG1": "0x00", + "REG2": "0x00", + "REG3": "0x00", + "MANDATE": "07/13/99", + "SERIALNO": "GS9229021308", + "BATTDATE": "13/11/15", + "NOMOUTV": "230 Volts", + "NOMBATTV": "24.0 Volts", + "EXTBATTS": "0", + "FIRMWARE": "70.11.I", + "END APC": "2016-02-09 21:13:26 +0000", +} diff --git a/tests/test_init.py b/tests/test_init.py index 30d01f6..b8c9603 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -32,7 +32,7 @@ async def test_request_raw_status(): def test_parse_raw_status(): - """Test parsing raw status""" + """Test parsing raw status.""" assert parse_raw_status(SAMPLE_STATUS) == PARSED_DICT