diff --git a/CHANGELOG.md b/CHANGELOG.md index 031db28..7d5f079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 1.2.0 +## 1.2.1 - Make async requests diff --git a/README.md b/README.md index ee8ac43..9029a12 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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__": diff --git a/pyecodevices/__init__.py b/pyecodevices/__init__.py index be25c07..8bf6dbf 100644 --- a/pyecodevices/__init__.py +++ b/pyecodevices/__init__.py @@ -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 @@ -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 @@ -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() @@ -72,7 +78,7 @@ 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: @@ -80,22 +86,14 @@ def host(self) -> str: 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.""" @@ -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.""" diff --git a/setup.py b/setup.py index be7653b..3122864 100644 --- a/setup.py +++ b/setup.py @@ -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.",