From 4736d24bd3acb22519f525f6294686f0ff090651 Mon Sep 17 00:00:00 2001 From: Tim Messerschmidt Date: Wed, 10 Mar 2021 22:26:53 +0100 Subject: [PATCH] Optional middleware (#6) * Formats code with black * Introduces the middleware parameter to handle https://github.com/home-assistant-ecosystem/python-volkszaehler/issues/5 --- setup.py | 46 ++++++++++++++++++++-------------------- volkszaehler/__init__.py | 27 +++++++++++++++++++---- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/setup.py b/setup.py index d6998e1..7b5ebed 100644 --- a/setup.py +++ b/setup.py @@ -10,36 +10,36 @@ here = os.path.abspath(os.path.dirname(__file__)) -with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f: +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') +if sys.argv[-1] == "publish": + os.system("python3 setup.py sdist upload") sys.exit() setup( - name='volkszaehler', - version='0.2.1', - description='Python Wrapper for interacting with the Volkszahler API.', + name="volkszaehler", + version="0.2.1", + description="Python Wrapper for interacting with the Volkszahler API.", long_description=long_description, - url='https://github.com/home-assistant-ecosystem/python-volkszaehler', - download_url='https://github.com/home-assistant-ecosystem/python-volkszaehler/releases', - author='Fabian Affolter', - author_email='fabian@affolter-engineering.ch', - license='MIT', - install_requires=['aiohttp', 'async_timeout'], - packages=['volkszaehler'], + url="https://github.com/home-assistant-ecosystem/python-volkszaehler", + download_url="https://github.com/home-assistant-ecosystem/python-volkszaehler/releases", + author="Fabian Affolter", + author_email="fabian@affolter-engineering.ch", + 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', + "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", ], ) diff --git a/volkszaehler/__init__.py b/volkszaehler/__init__.py index 56a5c46..1511e9e 100644 --- a/volkszaehler/__init__.py +++ b/volkszaehler/__init__.py @@ -9,20 +9,39 @@ _LOGGER = logging.getLogger(__name__) _RESOURCE = "{schema}://{host}:{port}/middleware.php/data/{uuid}.json" +_RESOURCE_NO_MIDDLEWARE = "{schema}://{host}:{port}/data/{uuid}.json" _RESOURCE_FROM = "from={param_from}" _RESOURCE_TO = "to={param_to}" + class Volkszaehler(object): """A class for handling the data retrieval.""" - def __init__(self, loop, session, uuid, host="localhost", port=80, tls=False, param_from="", param_to=""): + def __init__( + self, + loop, + session, + uuid, + host="localhost", + port=80, + tls=False, + param_from="", + param_to="", + middleware=True, + ): """Initialize the connection to the API.""" self._loop = loop self._session = session - if tls: - self.url = _RESOURCE.format(schema="https", host=host, port=port, uuid=uuid) + + if middleware: + self.url = _RESOURCE.format( + schema="https" if tls else "http", host=host, port=port, uuid=uuid + ) else: - self.url = _RESOURCE.format(schema="http", host=host, port=port, uuid=uuid) + self.url = _RESOURCE_NO_MIDDLEWARE.format( + schema="https" if tls else "http", host=host, port=port, uuid=uuid + ) + self.data = {} self.average = self.max = self.min = self.consumption = None self.tuples = []