Skip to content

Commit

Permalink
Get the values for one instance
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Aug 25, 2018
1 parent ea8b72a commit de41b44
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
30 changes: 30 additions & 0 deletions example.py
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())
49 changes: 49 additions & 0 deletions setup.py
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',
],
)
46 changes: 46 additions & 0 deletions volkszaehler/__init__.py
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']
23 changes: 23 additions & 0 deletions volkszaehler/exceptions.py
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

0 comments on commit de41b44

Please sign in to comment.