From 98dfc925f49c227cabe57098bb8dfe6c38a25de1 Mon Sep 17 00:00:00 2001 From: Matthieu Bourgain Date: Tue, 26 Jan 2021 18:09:14 +0100 Subject: [PATCH] move to xml api to add more informations --- CHANGELOG.md | 4 ++ README.md | 30 ++++++++--- pyecodevices/__init__.py | 107 ++++++++++++++++++++++++++------------- setup.py | 2 +- 4 files changed, 100 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6dc197..58ea2d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.1.0 + +- Use XML API of the Eco-Devices to get more information + ## 1.0.0 - Initial release diff --git a/README.md b/README.md index 8d7cae2..78912bc 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,24 @@ Get information from GCE Eco-Devices - `host`: ip or hostname - `port`: (default: 80) -- `username`: if authentication enabled -- `password`: if authentication enabled +- `username`: if authentication enabled on Eco-Devices +- `password`: if authentication enabled on Eco-Devices - `timeout`: (default: 3) +## Properties + +- `host`: return the host +- `firmware`: return the firmware version +- `mac_address`: return the mac address + ## Methods - `ping`: return true if the Eco-Devices answer - `global_get`: return json from the API -- `get`: return value of key parameter in: `current_t1`, `current_t2`, `daily_c1`, `daily_c2`, `total_c1`, `total_c2` +- `get_t1`: return values of input T1 +- `get_t2`: return values of input T2 +- `get_c1`: return values of input C1 +- `get_c2`: return values of input C2 ## Example @@ -22,7 +31,16 @@ Get information from GCE Eco-Devices from pyecodevices import EcoDevices ecodevices = EcoDevices('192.168.1.239','80',"username","password") -print (ecodevices.ping()) -print (ecodevices.global_get()) -print (ecodevices.get("current_t1")) + +print("# ping") +print(ecodevices.ping()) +print("# firmware version") +print(ecodevices.firmware) +print("# all values") +print(ecodevices.global_get()) +print("# inputs values") +print(ecodevices.get_t1()) +print(ecodevices.get_t2()) +print(ecodevices.get_c1()) +print(ecodevices.get_c2()) ``` diff --git a/pyecodevices/__init__.py b/pyecodevices/__init__.py index 47d51a5..e1a265b 100644 --- a/pyecodevices/__init__.py +++ b/pyecodevices/__init__.py @@ -1,69 +1,104 @@ -"""Get information from GCE Eco-Devices""" +"""Get information from GCE Eco-Devices.""" import requests +import xmltodict class EcoDevices: - """Class representing the Eco-Devices and its API""" + """Class representing the Eco-Devices and its XML API.""" - def __init__(self, host, port=80, username=None, password=None, timeout=2): + def __init__(self, host, port=80, username=None, password=None, timeout=3): + """Init a EcoDevice API.""" self._host = host self._port = port self._username = username self._password = password self._timeout = timeout - self._api_url = f"http://{host}:{port}/api/xdevices.json" + self._api_url = f"http://{host}:{port}/status.xml" - @property - def host(self): - return self._host - - def _request(self, params): + def _request(self): if self._username is not None and self._password is not None: r = requests.get( self._api_url, - params=params, + params={}, auth=(self._username, self._password), timeout=self._timeout, ) else: - r = requests.get(self._api_url, params=params, timeout=self._timeout) + r = requests.get(self._api_url, params={}, timeout=self._timeout) r.raise_for_status() - content = r.json() - product = content.get("product", None) - if product == "Eco-devices": - return content + xml_content = xmltodict.parse(r.text) + response = xml_content.get("response", None) + if response: + return response else: raise Exception( - "Eco-Devices api request error, url: %s`r%s", + "Eco-Devices XML request error, url: %s`r%s", r.request.url, - content, + response, ) + @property + def host(self): + """Return the hostname.""" + return self._host + + @property + def mac_address(self): + """Return the mac address.""" + return self._request().get("config_mac") + + @property + def firmware(self): + """Return the firmware.""" + return self._request().get("version") + def ping(self) -> bool: + """Return true if Eco-Devices answer to API request.""" try: - self._request({"cmd": 10}) + self._request() return True - except: + except Exception: pass return False def global_get(self): - return self._request({"cmd": 10}) + """Return all values from API.""" + return self._request() - def get(self, key) -> int: - """Get value from keys: current_t1, current_t2, daily_c1, daily_c2, total_c1, total_c2""" + def get_t1(self): + """Get values from teleinformation 1 input.""" + data = self._request() + return { + "current": data.get("T1_PAPP"), + "type_heures": data.get("T1_PTEC"), + "souscription": data.get("T1_ISOUSC"), + "intensite_max": data.get("T1_IMAX"), + } - if key == "current_t1": - return self._request({"cmd": 10}).get("T1_PAPP") - elif key == "current_t2": - return self._request({"cmd": 10}).get("T2_PAPP") - elif key == "daily_c1": - return self._request({"cmd": 20}).get("Day_C1") - elif key == "daily_c2": - return self._request({"cmd": 20}).get("Day_C2") - elif key == "total_c1": - return self._request({"cmd": 10}).get("INDEX_C1") - elif key == "total_c2": - return self._request({"cmd": 10}).get("INDEX_C2") - else: - raise Exception("key not found.") + def get_t2(self): + """Get values from teleinformation 1 input.""" + data = self._request() + return { + "current": data.get("T2_PAPP"), + "type_heures": data.get("T2_PTEC"), + "souscription": data.get("T2_ISOUSC"), + "intensite_max": data.get("T2_IMAX"), + } + + def get_c1(self): + """Get values from meter 1 input.""" + data = self._request() + return { + "daily": data.get("c0day"), + "total": data.get("count0"), + "fuel": data.get("c0_fuel"), + } + + def get_c2(self): + """Get values from meter 2 input.""" + data = self._request() + return { + "daily": data.get("c1day"), + "total": data.get("count1"), + "fuel": data.get("c1_fuel"), + } diff --git a/setup.py b/setup.py index 8b9c1d6..c0ad693 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pyecodevices", - version="1.0.0", + version="1.1.0", author="Aohzan", author_email="aohzan@gmail.com", description="Get information from GCE Eco-Devices.",