From b19d1a2fe6768b8467d941f243aff312ace9221d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 18 Aug 2021 23:28:13 -0700 Subject: [PATCH] Fixes (#3) --- aioeagle/__init__.py | 1 + aioeagle/electric_meter.py | 20 +++++++++---- aioeagle/util.py | 16 ++++++----- bla.py | 59 -------------------------------------- example.py | 9 ++++-- requirements.txt | 1 - setup.py | 2 +- 7 files changed, 33 insertions(+), 75 deletions(-) delete mode 100644 bla.py diff --git a/aioeagle/__init__.py b/aioeagle/__init__.py index de31a6a..182b50e 100644 --- a/aioeagle/__init__.py +++ b/aioeagle/__init__.py @@ -2,3 +2,4 @@ from .hub import EagleHub # noqa: F401 from .errors import * # noqa: F403, F401 +from .electric_meter import ElectricMeter # noqa: F401 diff --git a/aioeagle/electric_meter.py b/aioeagle/electric_meter.py index fe9328e..4c22f8c 100644 --- a/aioeagle/electric_meter.py +++ b/aioeagle/electric_meter.py @@ -5,7 +5,7 @@ class ElectricMeter: """Represent an electric meter. {'ConnectionStatus': 'Connected', - 'HardwareAddress': '0x00078100023076c8', + 'HardwareAddress': '0x00abcd123', 'LastContact': '0x611d885a', 'Manufacturer': 'Generic', 'ModelId': 'electric_meter', @@ -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 @@ -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): @@ -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"] diff --git a/aioeagle/util.py b/aioeagle/util.py index 23e2d42..82f08cd 100644 --- a/aioeagle/util.py +++ b/aioeagle/util.py @@ -1,6 +1,3 @@ -from dicttoxml import dicttoxml as dicttoxml_orig - - def xmltodict_ensure_list(value, key): """Make sure response is a list @@ -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 @@ -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)}" for key, val in value.items()) + + if isinstance(value, list): + return "".join(dicttoxml(val) for val in value) + + return value diff --git a/bla.py b/bla.py deleted file mode 100644 index 8e831d9..0000000 --- a/bla.py +++ /dev/null @@ -1,59 +0,0 @@ -import xmltodict -import dicttoxml - -from pprint import pprint -from aioeagle.electric_meter import ElectricMeter - -meter = ElectricMeter({"HardwareAddress": "mock hw address"}, None) - -components = { - "Component": { - "Name": "Main", - "Variables": [{"Name": var} for var in meter.ENERGY_AND_POWER_VARIABLES], - } -} - -print( - meter.create_command( - "device_query", {"Components": components}, {"item_func": lambda _: "Variable"} - ) -) - - -# pprint( -# xmltodict.parse( -# """ -# -# - -# """, -# dict_constructor=dict, -# ) -# ) - - -# def item_func(parent): -# print("parent", parent) -# return "Variable" - - -# print( -# dicttoxml.dicttoxml( -# { -# "Components": { -# "Component": { -# "Name": "Main", -# "Variables": [ -# {"Name": "bla"}, -# {"Name": "bla"}, -# {"Name": "bla"}, -# {"Name": "bla"}, -# ], -# } -# } -# }, -# root=False, -# attr_type=False, -# item_func=item_func, -# ) -# ) diff --git a/example.py b/example.py index 2221204..e96f7c9 100644 --- a/example.py +++ b/example.py @@ -17,7 +17,12 @@ async def run(websession): print(f"Usage: {sys.argv[0]} ") 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() @@ -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: diff --git a/requirements.txt b/requirements.txt index 42e4512..55b0713 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ aiohttp xmltodict -dicttoxml diff --git a/setup.py b/setup.py index a15e489..dc0fc8d 100644 --- a/setup.py +++ b/setup.py @@ -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",