-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Copyright (c) 2018 Fabian Affolter <[email protected]> | ||
Licensed under MIT. All rights reserved. | ||
""" | ||
import asyncio | ||
|
||
import aiohttp | ||
|
||
from volkszaehler import Volkszaehler | ||
|
||
HOST = 'demo.volkszaehler.org' | ||
UUID = '57acbef0-88a9-11e4-934f-6b0f9ecd95a8' | ||
|
||
|
||
async def main(): | ||
"""The main part of the example script.""" | ||
async with aiohttp.ClientSession() as session: | ||
zaehler = Volkszaehler(loop, session, UUID, host=HOST) | ||
|
||
# Get the data | ||
await zaehler.get_data() | ||
|
||
print("Average:", zaehler.average) | ||
print("Max:", zaehler.max) | ||
print("Min:", zaehler.min) | ||
print("Consumption:", zaehler.consumption) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Copyright (c) 2018 Fabian Affolter <[email protected]> | ||
Licensed under MIT. All rights reserved. | ||
""" | ||
import os | ||
import sys | ||
|
||
try: | ||
from setuptools import setup | ||
except ImportError: | ||
from distutils.core import setup | ||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f: | ||
long_description = f.read() | ||
|
||
if sys.argv[-1] == 'publish': | ||
os.system('python3 setup.py sdist upload') | ||
sys.exit() | ||
|
||
setup( | ||
name='volkszaehler', | ||
version='0.1.0', | ||
description='Python Wrapper for interacting with the Volkszahler API.', | ||
long_description=long_description, | ||
url='https://github.com/fabaff/python-volkszaehler', | ||
download_url='https://github.com/fabaff/python-volkszaehler/releases', | ||
author='Fabian Affolter', | ||
author_email='[email protected]', | ||
license='MIT', | ||
install_requires=['aiohttp', 'async_timeout'], | ||
packages=['volkszaehler'], | ||
zip_safe=True, | ||
classifiers=[ | ||
'Development Status :: 3 - Alpha', | ||
'Environment :: Console', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: MIT License', | ||
'Operating System :: MacOS :: MacOS X', | ||
'Operating System :: Microsoft :: Windows', | ||
'Operating System :: POSIX', | ||
'Programming Language :: Python :: 3.5', | ||
'Programming Language :: Python :: 3.6', | ||
'Topic :: Utilities', | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Copyright (c) 2018 Fabian Affolter <[email protected]> | ||
Licensed under MIT. All rights reserved. | ||
""" | ||
import asyncio | ||
import logging | ||
|
||
import aiohttp | ||
import async_timeout | ||
|
||
from . import exceptions | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
_RESOURCE = 'http://{host}:{port}/middleware.php/data/{uuid}.json' | ||
|
||
|
||
class Volkszaehler(object): | ||
"""A class for handling the data retrieval.""" | ||
|
||
def __init__(self, loop, session, uuid, host='localhost', port=80): | ||
"""Initialize the connection to the API.""" | ||
self._loop = loop | ||
self._session = session | ||
self.url = _RESOURCE.format(host=host, port=port, uuid=uuid) | ||
self.data = {} | ||
self.average = self.max = self.min = self.consumption = None | ||
|
||
async def get_data(self): | ||
"""Retrieve the data.""" | ||
try: | ||
with async_timeout.timeout(5, loop=self._loop): | ||
response = await self._session.get(self.url) | ||
|
||
_LOGGER.debug( | ||
"Response from Volkszaehler API: %s", response.status) | ||
self.data = await response.json() | ||
_LOGGER.debug(self.data) | ||
except (asyncio.TimeoutError, aiohttp.ClientError): | ||
_LOGGER.error("Can not load data from Volkszaehler API") | ||
raise exceptions.VolkszaehlerApiConnectionError() | ||
|
||
self.average = self.data['data']['average'] | ||
self.max = self.data['data']['max'][1] | ||
self.min = self.data['data']['min'][1] | ||
self.consumption = self.data['data']['consumption'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
Copyright (c) 2018 Fabian Affolter <[email protected]> | ||
Licensed under MIT. All rights reserved. | ||
""" | ||
|
||
|
||
class VolkszaehlerError(Exception): | ||
"""General Volkszaehler Error exception occurred.""" | ||
|
||
pass | ||
|
||
|
||
class VolkszaehlerApiConnectionError(VolkszaehlerError): | ||
"""When a connection error is encountered.""" | ||
|
||
pass | ||
|
||
|
||
class VolkszaehlerNoDataAvailable(VolkszaehlerError): | ||
"""When no data is available.""" | ||
|
||
pass |