diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index fabd5ab..78a0557 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -25,7 +25,7 @@ jobs: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | - python -m pip install --upgrade pip + python -m pip install --upgrade pip setuptools wheel pip install -r requirements.txt - name: Lint with flake8 run: | diff --git a/examples/example-bulb-hsv.py b/examples/example-bulb-hsv.py index 171368c..5b7e702 100644 --- a/examples/example-bulb-hsv.py +++ b/examples/example-bulb-hsv.py @@ -1,4 +1,5 @@ """Example code for communicating with a myStrom bulb and HSV values.""" + import time from pymystrom import bulb diff --git a/examples/example-bulb.py b/examples/example-bulb.py index e771423..84c5a92 100644 --- a/examples/example-bulb.py +++ b/examples/example-bulb.py @@ -1,12 +1,13 @@ """Example code for communicating with a myStrom bulb.""" + import asyncio import logging from pymystrom.bulb import MyStromBulb from pymystrom.discovery import discover_devices -IP_ADDRESS = "192.168.0.51" -MAC_ADDRESS = "5CCF7FA0AFB0" +IP_ADDRESS = "192.168.1.25" +MAC_ADDRESS = "5CCF7FA0C27C" async def main(): diff --git a/examples/example-discovery.py b/examples/example-discovery.py new file mode 100644 index 0000000..76e94df --- /dev/null +++ b/examples/example-discovery.py @@ -0,0 +1,15 @@ +import asyncio + +from pymystrom import discovery + + +async def main(): + """Sample code to work with discovery.""" + # Discover all bulbs in the network via broadcast datagram (UDP) + bulbs = await discovery.discover_devices() + print(f"Number of detected bulbs: {len(bulbs)}") + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/examples/example-get-data-bulb.py b/examples/example-get-data-bulb.py index c6f03ac..c0b07d3 100644 --- a/examples/example-get-data-bulb.py +++ b/examples/example-get-data-bulb.py @@ -1,4 +1,5 @@ """Example code for getting the data from a myStrom bulb.""" + from pymystrom import bulb bulb = bulb.MyStromBulb("192.168.0.51", "5CCF7FA0AFB0") diff --git a/examples/example-pir.py b/examples/example-pir.py index 7596a5a..c884e20 100644 --- a/examples/example-pir.py +++ b/examples/example-pir.py @@ -1,4 +1,5 @@ """Example code for communicating with a myStrom PIR unit.""" + import asyncio from pymystrom.pir import MyStromPir diff --git a/examples/example-switch.py b/examples/example-switch.py index 8e78c04..1287379 100644 --- a/examples/example-switch.py +++ b/examples/example-switch.py @@ -1,4 +1,5 @@ """Example code for communicating with a myStrom plug/switch.""" + import asyncio from pymystrom.switch import MyStromSwitch diff --git a/pymystrom/__init__.py b/pymystrom/__init__.py index 80cc054..e81c6a8 100644 --- a/pymystrom/__init__.py +++ b/pymystrom/__init__.py @@ -1,18 +1,16 @@ """Base details for the myStrom Python bindings.""" + import asyncio -import aiohttp -import async_timeout -from yarl import URL -from typing import Any, Mapping, Optional import socket -from .exceptions import MyStromConnectionError +from typing import Any, Mapping, Optional -import pkg_resources +import aiohttp +from yarl import URL -__version__ = pkg_resources.get_distribution("setuptools").version +from .exceptions import MyStromConnectionError TIMEOUT = 10 -USER_AGENT = f"PythonMyStrom/{__version__}" +USER_AGENT = "PythonMyStrom/1.0" async def _request( @@ -34,15 +32,17 @@ async def _request( self._close_session = True try: - with async_timeout.timeout(TIMEOUT): - response = await self._session.request( + response = await asyncio.wait_for( + self._session.request( method, uri, data=data, json=json_data, params=params, headers=headers, - ) + ), + timeout=TIMEOUT, + ) except asyncio.TimeoutError as exception: raise MyStromConnectionError( "Timeout occurred while connecting to myStrom device." diff --git a/pymystrom/bulb.py b/pymystrom/bulb.py index 7934485..adfa70f 100644 --- a/pymystrom/bulb.py +++ b/pymystrom/bulb.py @@ -1,10 +1,11 @@ """Support for communicating with myStrom bulbs.""" + import asyncio import logging +from typing import Optional import aiohttp from yarl import URL -from typing import Optional from . import _request as request diff --git a/pymystrom/cli.py b/pymystrom/cli.py index 3ebd1d0..b76d034 100644 --- a/pymystrom/cli.py +++ b/pymystrom/cli.py @@ -1,4 +1,5 @@ """Command-line tool for working with myStrom devices.""" + import click import requests import asyncio diff --git a/pymystrom/discovery.py b/pymystrom/discovery.py index e9956f9..5389062 100644 --- a/pymystrom/discovery.py +++ b/pymystrom/discovery.py @@ -1,7 +1,8 @@ """Support for discovering myStrom devices.""" + import asyncio import logging -from typing import Optional, List +from typing import List, Optional from .device_types import DEVICE_MAPPING_NUMERIC diff --git a/pymystrom/pir.py b/pymystrom/pir.py index de66b29..dc68441 100644 --- a/pymystrom/pir.py +++ b/pymystrom/pir.py @@ -1,7 +1,9 @@ """Support for communicating with myStrom PIRs.""" + +from typing import Any, Dict, Iterable, List, Optional, Union + import aiohttp from yarl import URL -from typing import Any, Dict, Iterable, List, Optional, Union from . import _request as request diff --git a/pymystrom/switch.py b/pymystrom/switch.py index 30303a7..d83b088 100644 --- a/pymystrom/switch.py +++ b/pymystrom/switch.py @@ -1,10 +1,12 @@ """Support for communicating with myStrom plugs/switches.""" + +from typing import Optional, Union + import aiohttp from yarl import URL -from typing import Optional, Union from . import _request as request -from .device_types import DEVICE_MAPPING_NUMERIC, DEVICE_MAPPING_LITERAL +from .device_types import DEVICE_MAPPING_LITERAL, DEVICE_MAPPING_NUMERIC class MyStromSwitch: diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..97eee23 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,26 @@ +[tool.poetry] +name = "python-mystrom" +version = "2.4.0" +description = "Asynchronous Python API client for interacting with myStrom devices" +authors = ["Fabian Affolter "] +license = "MIT" +readme = "README.rst" +homepage = "https://github.com/home-assistant-ecosystem/python-mystrom" +repository = "https://github.com/home-assistant-ecosystem/python-mystrom" +keywords = ["myStrom", "API", "client", "asynchronous"] +packages = [ + { include = "pymystrom" } +] + +[tool.poetry.dependencies] +python = ">=3.11" +aiohttp = "*" +click = "*" +requests = "*" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +pymystrom = "pymystrom.cli:main" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index bd1170e..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -# python-mystrom requirements. Use setup.py for the dependencies. -. diff --git a/setup.py b/setup.py deleted file mode 100644 index b3d7b59..0000000 --- a/setup.py +++ /dev/null @@ -1,49 +0,0 @@ -"""Set up the Python API for myStrom devices.""" -import os - -import sys - -from setuptools import setup, find_packages - -here = os.path.abspath(os.path.dirname(__file__)) - -with open(os.path.join(here, "README.rst"), encoding="utf-8") as readme: - long_description = readme.read() - -setup( - name="python-mystrom", - version="2.3.0", - description="Asynchronous Python API client for interacting with myStrom devices", - long_description=long_description, - url="https://github.com/home-assistant-ecosystem/python-mystrom", - author="Fabian Affolter", - author_email="fabian@affolter-engineering.ch", - license="MIT", - install_requires=[ - "requests", - "click", - "aiohttp", - "setuptools", - "async_timeout", - ], - packages=find_packages(), - python_requires=">=3.9", - zip_safe=True, - include_package_data=True, - entry_points=""" - [console_scripts] - mystrom=pymystrom.cli:main -""", - 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.9", - "Programming Language :: Python :: 3.10", - "Topic :: Utilities", - ], -)