Handle dbus single byte (#2237)

This commit is contained in:
Pascal Vizeli 2020-11-11 13:09:13 +01:00 committed by GitHub
parent 0a7c75830b
commit c4847ad10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 7 deletions

View File

@ -44,7 +44,7 @@ class NetworkWirelessAP(DBusInterfaceProxy):
@property @property
def strength(self) -> int: def strength(self) -> int:
"""Return details about mac address.""" """Return details about mac address."""
return int(self.properties[DBUS_ATTR_STRENGTH]) return int(self.properties[DBUS_ATTR_STRENGTH][0])
async def connect(self) -> None: async def connect(self) -> None:
"""Get connection information.""" """Get connection information."""

View File

@ -150,7 +150,7 @@ class NetworkManager(CoreSysAttributes):
try: try:
await accesspoint.connect() await accesspoint.connect()
except DBusError as err: except DBusError as err:
_LOGGER.waring("Can't process an AP: %s", err) _LOGGER.warning("Can't process an AP: %s", err)
continue continue
else: else:
accesspoints.append( accesspoints.append(

View File

@ -34,7 +34,7 @@ RE_GVARIANT_STRING: re.Pattern[Any] = re.compile(
r"(?<=(?: |{|\[|\(|<))'(.*?)'(?=(?:|]|}|,|\)|>))" r"(?<=(?: |{|\[|\(|<))'(.*?)'(?=(?:|]|}|,|\)|>))"
) )
RE_GVARIANT_BINARY: re.Pattern[Any] = re.compile( RE_GVARIANT_BINARY: re.Pattern[Any] = re.compile(
r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|\[byte (.*?)\]|\[(0x[0-9A-Za-z]{2}.*?)\]" r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|\[byte (.*?)\]|\[(0x[0-9A-Za-z]{2}.*?)\]|<byte (.*?)>"
) )
RE_GVARIANT_BINARY_STRING: re.Pattern[Any] = re.compile( RE_GVARIANT_BINARY_STRING: re.Pattern[Any] = re.compile(
r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|<?b\'(.*?)\'>?" r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|<?b\'(.*?)\'>?"
@ -143,8 +143,8 @@ class DBus:
# Handle Bytes # Handle Bytes
json_raw = RE_GVARIANT_BINARY.sub( json_raw = RE_GVARIANT_BINARY.sub(
lambda x: x.group(0) lambda x: x.group(0)
if not (x.group(1) or x.group(2)) if not (x.group(1) or x.group(2) or x.group(3))
else _convert_bytes(x.group(1) or x.group(2)), else _convert_bytes(x.group(1) or x.group(2) or x.group(3)),
json_raw, json_raw,
) )
json_raw = RE_GVARIANT_BINARY_STRING.sub( json_raw = RE_GVARIANT_BINARY_STRING.sub(

View File

@ -7,6 +7,6 @@
"HwAddress": "E4:57:40:A9:D7:DE", "HwAddress": "E4:57:40:A9:D7:DE",
"Mode": 2, "Mode": 2,
"MaxBitrate": 195000, "MaxBitrate": 195000,
"Strength": 47, "Strength": [47],
"LastSeen": 1398776 "LastSeen": 1398776
} }

View File

@ -7,6 +7,6 @@
"HwAddress": "18:4B:0D:23:A1:9C", "HwAddress": "18:4B:0D:23:A1:9C",
"Mode": 2, "Mode": 2,
"MaxBitrate": 540000, "MaxBitrate": 540000,
"Strength": 63, "Strength": [63],
"LastSeen": 1398839 "LastSeen": 1398839
} }

View File

@ -473,3 +473,25 @@ def test_v6():
], ],
} }
] ]
def test_single_byte():
"""Test a singlebyte response."""
raw = "({'Flags': <uint32 1>, 'WpaFlags': <uint32 0>, 'RsnFlags': <uint32 392>, 'Ssid': <[byte 0x53, 0x59, 0x53, 0x48, 0x41, 0x43, 0x4b, 0x5f, 0x48, 0x6f, 0x6d, 0x65]>, 'Frequency': <uint32 5660>, 'HwAddress': <'18:4B:0D:A3:A1:9C'>, 'Mode': <uint32 2>, 'MaxBitrate': <uint32 540000>, 'Strength': <byte 0x2c>, 'LastSeen': <1646569>},)"
data = DBus.parse_gvariant(raw)
assert data == [
{
"Flags": 1,
"Frequency": 5660,
"HwAddress": "18:4B:0D:A3:A1:9C",
"LastSeen": 1646569,
"MaxBitrate": 540000,
"Mode": 2,
"RsnFlags": 392,
"Ssid": [83, 89, 83, 72, 65, 67, 75, 95, 72, 111, 109, 101],
"Strength": [44],
"WpaFlags": 0,
}
]