Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Apr 12, 2021
1 parent 3bdced0 commit 5c2ed76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## 1.2.0
## 1.2.1

- Make async requests

Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Get information from GCE Eco-Devices
- `port`: (default: 80)
- `username`: if authentication enabled on Eco-Devices
- `password`: if authentication enabled on Eco-Devices
- `request_timeout`: (default: 3)
- `request_timeout`: (default: 10)

## Properties

- `host`: return the host
- `firmware`: return the firmware version
- `version`: 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_info`: get properties from the API
- `global_get`: return all data from the API
- `get_t1`: return values of input T1
- `get_t2`: return values of input T2
- `get_c1`: return values of input C1
Expand All @@ -35,14 +35,12 @@ import asyncio

async def main():
async with EcoDevices('192.168.1.239', '80', "username", "password") as ecodevices:
ping = await ecodevices.ping()
print("ping:", ping)
version = await ecodevices.firmware
print("firmware version: ", version)
await ecodevices.get_info()
print("firmware version:", ecodevices.version)
data = await ecodevices.global_get()
print("all values: ", data)
print("all values:", data)
data = await ecodevices.get_t1()
print("teleinfo 1: ", data["current"])
print("teleinfo 1:", data["current"], "VA")


if __name__ == "__main__":
Expand Down
42 changes: 20 additions & 22 deletions pyecodevices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
username: str = None,
password: str = None,
request_timeout: int = 10,
session: aiohttp.client.ClientSession = None
session: aiohttp.client.ClientSession = None,
) -> None:
"""Init a EcoDevice API."""
self._host = host
Expand All @@ -26,10 +26,18 @@ def __init__(
self._password = password
self._request_timeout = request_timeout
self._api_url = f"http://{host}:{port}/status.xml"
self._version = None
self._mac_address = None

self._session = session
self._close_session = False

async def get_info(self):
"""Get properties from API."""
init_data = await self._request()
self._version = init_data["version"]
self._mac_address = init_data["config_mac"]

async def _request(self) -> dict:
"""Make a request to get Eco-Devices data."""
auth = None
Expand All @@ -53,17 +61,15 @@ async def _request(self) -> dict:
ssl=False,
)
except asyncio.TimeoutError as exception:
raise CannotConnectError(
raise EcoDevicesCannotConnectError(
"Timeout occurred while connecting to Eco-Devices."
) from exception
except (aiohttp.ClientError, socket.gaierror) as exception:
raise CannotConnectError(
raise EcoDevicesCannotConnectError(
"Error occurred while communicating with Eco-Devices."
) from exception
if response.status == 401:
raise InvalidAuthError(
"Authentication failed with Eco-Devices."
)
raise EcoDevicesInvalidAuthError("Authentication failed with Eco-Devices.")

if response.status:
contents = await response.text()
Expand All @@ -72,30 +78,22 @@ async def _request(self) -> dict:
data = xml_content.get("response", None)
if data:
return data
raise CannotConnectError("Eco-Devices XML request error:", data)
raise EcoDevicesCannotConnectError("Eco-Devices XML request error:", data)

@property
def host(self) -> str:
"""Return the hostname."""
return self._host

@property
async def mac_address(self) -> str:
def mac_address(self) -> str:
"""Return the mac address."""
data = await self._request()
return data["config_mac"]
return self._mac_address

@property
async def firmware(self) -> str:
"""Return the mac address."""
data = await self._request()
return data["version"]

async def ping(self) -> bool:
"""Return true if Eco-Devices answer to API request."""
if await self._request():
return True
return False
def version(self) -> str:
"""Return the firmware version."""
return self._version

async def global_get(self) -> dict:
"""Return all values from API."""
Expand Down Expand Up @@ -153,9 +151,9 @@ async def __aexit__(self, *_exc_info) -> None:
await self.close()


class CannotConnectError(Exception):
class EcoDevicesCannotConnectError(Exception):
"""Exception to indicate an error in connection."""


class InvalidAuthError(Exception):
class EcoDevicesInvalidAuthError(Exception):
"""Exception to indicate an error in authentication."""
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.2.0",
version="1.2.1",
author="Aohzan",
author_email="aohzan@gmail.com",
description="Get information from GCE Eco-Devices.",
Expand Down

0 comments on commit 5c2ed76

Please sign in to comment.