Skip to content

Commit

Permalink
connection.py: fix device_query api to return dict
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Rusak <lorusak@gmail.com>
  • Loading branch information
lrusak committed May 15, 2024
1 parent c08799c commit d8fc1d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 13 additions & 7 deletions libeagle/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def device_details(self, address) -> dict:

return details

async def device_query(self, address, component_name=None, variable_name=None) -> list[dict]:
async def device_query(self, address, component_name=None, variable_name=None) -> dict:
"""
Returns the device query for a given hardware address, name, and variable
Expand Down Expand Up @@ -180,25 +180,31 @@ async def device_query(self, address, component_name=None, variable_name=None) -

self.logger.debug(f"return data: {etree.tostring(xml).decode()}")

data = []
query = {"Components": []}

components = xml.find("Components")
for component in components.iter("Component"):
device_details = xml.find("DeviceDetails")
for detail in device_details.iter():
query[detail.tag] = detail.text

for component in xml.findall("Component"):

component_data = {}

for detail in component.iter():

if detail.tag == "Variables":
component_data["Variables"] = {}
component_data["Variables"] = []

for variable in detail.iter("Variable"):
key = variable.findtext("Name")
value = variable.findtext("Value")
component_data["Variables"][key] = value
else:
component_data[detail.tag] = detail.text

data.append(component_data)
query["Components"].append(component_data)

return data
return query

async def _doRequest(self, values) -> bytes:
async with self.session.post("/cgi-bin/post_manager", data=values) as response:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_eagle200.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ async def test_eagle200(self):
)

assert len(query) > 0
assert "Name" in query
assert "HardwareAddress" in query
assert "Components" in query

for component in query:
for component in query["Components"]:

assert "Variables" in component
assert "zigbee:InstantaneousDemand" in component["Variables"]
Expand All @@ -65,8 +68,11 @@ async def test_eagle200(self):
query = await conn.device_query(device["HardwareAddress"])

assert len(query) > 0
assert "Name" in query
assert "HardwareAddress" in query
assert "Components" in query

for component in query:
for component in query["Components"]:

assert "Variables" in component
assert "zigbee:Message" in component["Variables"]
Expand Down

0 comments on commit d8fc1d0

Please sign in to comment.