Skip to content

Commit

Permalink
Fixes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Aug 19, 2021
1 parent 6e17f08 commit b19d1a2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 75 deletions.
1 change: 1 addition & 0 deletions aioeagle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from .hub import EagleHub # noqa: F401
from .errors import * # noqa: F403, F401
from .electric_meter import ElectricMeter # noqa: F401
20 changes: 15 additions & 5 deletions aioeagle/electric_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ElectricMeter:
"""Represent an electric meter.
{'ConnectionStatus': 'Connected',
'HardwareAddress': '0x00078100023076c8',
'HardwareAddress': '0x00abcd123',
'LastContact': '0x611d885a',
'Manufacturer': 'Generic',
'ModelId': 'electric_meter',
Expand All @@ -14,12 +14,24 @@ class ElectricMeter:
'Protocol': 'Zigbee'}
"""

# Bug in the API is causing CurrentSummationDelivered not
# to be returned. So just fetch all (default behavior)
ENERGY_AND_POWER_VARIABLES = [
"zigbee:InstantaneousDemand",
"zigbee:CurrentSummationDelivered",
"zigbee:CurrentSummationReceived",
]

@classmethod
def create_instance(cls, hub, hardware_address):
"""Create a new electric meter."""
return cls(
{
"HardwareAddress": hardware_address,
},
hub.make_request,
)

def __init__(self, details, make_request):
"""Initialize the electric meter."""
self.details = details
Expand Down Expand Up @@ -62,12 +74,11 @@ def network_address(self) -> str:
def protocol(self) -> str:
return self.details["Protocol"]

def create_command(self, command, extra_data={}, dicttoxml_args={}):
def create_command(self, command, extra_data={}):
"""Create command targeting this device."""
return create_command(
command,
{"DeviceDetails": {"HardwareAddress": self.hardware_address}, **extra_data},
dicttoxml_args,
)

async def get_device_details(self):
Expand All @@ -86,14 +97,13 @@ async def get_device_query(self, variables=None):
components = {
"Component": {
"Name": "Main",
"Variables": [{"Name": var} for var in variables],
"Variables": [{"Variable": {"Name": var}} for var in variables],
}
}
data = await self.make_request(
self.create_command(
"device_query",
{"Components": components},
{"item_func": lambda _: "Variable"},
)
)
self.details = data["Device"]["DeviceDetails"]
Expand Down
16 changes: 9 additions & 7 deletions aioeagle/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from dicttoxml import dicttoxml as dicttoxml_orig


def xmltodict_ensure_list(value, key):
"""Make sure response is a list
Expand All @@ -18,7 +15,7 @@ def xmltodict_ensure_list(value, key):
return [value]


def create_command(command, extra_data={}, dicttoxml_args={}):
def create_command(command, extra_data={}):
"""Create a basic command string
This is used to create a command string that can be used
Expand All @@ -31,9 +28,14 @@ def create_command(command, extra_data={}, dicttoxml_args={}):
**extra_data,
},
},
**dicttoxml_args,
)


def dicttoxml(value, **kwargs):
return dicttoxml_orig(value, root=False, attr_type=False, **kwargs)
def dicttoxml(value):
if isinstance(value, dict):
return "".join(f"<{key}>{dicttoxml(val)}</{key}>" for key, val in value.items())

if isinstance(value, list):
return "".join(dicttoxml(val) for val in value)

return value
59 changes: 0 additions & 59 deletions bla.py

This file was deleted.

9 changes: 7 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ async def run(websession):
print(f"Usage: {sys.argv[0]} <cloud_id> <install_code>")
return

hub = EagleHub(websession, sys.argv[1], sys.argv[2])
kwargs = {}

if len(sys.argv) > 3:
kwargs["host"] = sys.argv[3]

hub = EagleHub(websession, sys.argv[1], sys.argv[2], **kwargs)

devices = await hub.get_device_list()

Expand All @@ -29,7 +34,7 @@ async def run(websession):

pprint(device.details)
print()
pprint(await device.get_device_query(device.ENERGY_AND_POWER_VARIABLES))
pprint(await device.get_device_query())


try:
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
aiohttp
xmltodict
dicttoxml
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="aioeagle",
version="1.0.0",
version="1.1.0",
license="Apache License 2.0",
url="https://github.com/home-assistant-libs/aioeagle",
author="Paulus Schoutsen",
Expand Down

0 comments on commit b19d1a2

Please sign in to comment.