Skip to content

Commit

Permalink
move to xml api to add more informations
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Jan 26, 2021
1 parent 3d147ae commit 98dfc92
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.0

- Use XML API of the Eco-Devices to get more information

## 1.0.0

- Initial release
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,41 @@ 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

```python
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())
```
107 changes: 71 additions & 36 deletions pyecodevices/__init__.py
Original file line number Diff line number Diff line change
@@ -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"),
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 98dfc92

Please sign in to comment.