Skip to content

Commit de41b44

Browse files
committed
Get the values for one instance
1 parent ea8b72a commit de41b44

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Copyright (c) 2018 Fabian Affolter <[email protected]>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
import asyncio
7+
8+
import aiohttp
9+
10+
from volkszaehler import Volkszaehler
11+
12+
HOST = 'demo.volkszaehler.org'
13+
UUID = '57acbef0-88a9-11e4-934f-6b0f9ecd95a8'
14+
15+
16+
async def main():
17+
"""The main part of the example script."""
18+
async with aiohttp.ClientSession() as session:
19+
zaehler = Volkszaehler(loop, session, UUID, host=HOST)
20+
21+
# Get the data
22+
await zaehler.get_data()
23+
24+
print("Average:", zaehler.average)
25+
print("Max:", zaehler.max)
26+
print("Min:", zaehler.min)
27+
print("Consumption:", zaehler.consumption)
28+
29+
loop = asyncio.get_event_loop()
30+
loop.run_until_complete(main())

setup.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Copyright (c) 2018 Fabian Affolter <[email protected]>
4+
5+
Licensed under MIT. All rights reserved.
6+
"""
7+
import os
8+
import sys
9+
10+
try:
11+
from setuptools import setup
12+
except ImportError:
13+
from distutils.core import setup
14+
15+
here = os.path.abspath(os.path.dirname(__file__))
16+
17+
with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
18+
long_description = f.read()
19+
20+
if sys.argv[-1] == 'publish':
21+
os.system('python3 setup.py sdist upload')
22+
sys.exit()
23+
24+
setup(
25+
name='volkszaehler',
26+
version='0.1.0',
27+
description='Python Wrapper for interacting with the Volkszahler API.',
28+
long_description=long_description,
29+
url='https://github.com/fabaff/python-volkszaehler',
30+
download_url='https://github.com/fabaff/python-volkszaehler/releases',
31+
author='Fabian Affolter',
32+
author_email='[email protected]',
33+
license='MIT',
34+
install_requires=['aiohttp', 'async_timeout'],
35+
packages=['volkszaehler'],
36+
zip_safe=True,
37+
classifiers=[
38+
'Development Status :: 3 - Alpha',
39+
'Environment :: Console',
40+
'Intended Audience :: Developers',
41+
'License :: OSI Approved :: MIT License',
42+
'Operating System :: MacOS :: MacOS X',
43+
'Operating System :: Microsoft :: Windows',
44+
'Operating System :: POSIX',
45+
'Programming Language :: Python :: 3.5',
46+
'Programming Language :: Python :: 3.6',
47+
'Topic :: Utilities',
48+
],
49+
)

volkszaehler/__init__.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Copyright (c) 2018 Fabian Affolter <[email protected]>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
import asyncio
7+
import logging
8+
9+
import aiohttp
10+
import async_timeout
11+
12+
from . import exceptions
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
_RESOURCE = 'http://{host}:{port}/middleware.php/data/{uuid}.json'
16+
17+
18+
class Volkszaehler(object):
19+
"""A class for handling the data retrieval."""
20+
21+
def __init__(self, loop, session, uuid, host='localhost', port=80):
22+
"""Initialize the connection to the API."""
23+
self._loop = loop
24+
self._session = session
25+
self.url = _RESOURCE.format(host=host, port=port, uuid=uuid)
26+
self.data = {}
27+
self.average = self.max = self.min = self.consumption = None
28+
29+
async def get_data(self):
30+
"""Retrieve the data."""
31+
try:
32+
with async_timeout.timeout(5, loop=self._loop):
33+
response = await self._session.get(self.url)
34+
35+
_LOGGER.debug(
36+
"Response from Volkszaehler API: %s", response.status)
37+
self.data = await response.json()
38+
_LOGGER.debug(self.data)
39+
except (asyncio.TimeoutError, aiohttp.ClientError):
40+
_LOGGER.error("Can not load data from Volkszaehler API")
41+
raise exceptions.VolkszaehlerApiConnectionError()
42+
43+
self.average = self.data['data']['average']
44+
self.max = self.data['data']['max'][1]
45+
self.min = self.data['data']['min'][1]
46+
self.consumption = self.data['data']['consumption']

volkszaehler/exceptions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Copyright (c) 2018 Fabian Affolter <[email protected]>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
7+
8+
class VolkszaehlerError(Exception):
9+
"""General Volkszaehler Error exception occurred."""
10+
11+
pass
12+
13+
14+
class VolkszaehlerApiConnectionError(VolkszaehlerError):
15+
"""When a connection error is encountered."""
16+
17+
pass
18+
19+
20+
class VolkszaehlerNoDataAvailable(VolkszaehlerError):
21+
"""When no data is available."""
22+
23+
pass

0 commit comments

Comments
 (0)