pax_global_header00006660000000000000000000000064146563220640014522gustar00rootroot0000000000000052 comment=b463ac6101c25b027ecfb62c3d4edcc5bfbf4379 pycoolmasternet-async-0.2.2/000077500000000000000000000000001465632206400160665ustar00rootroot00000000000000pycoolmasternet-async-0.2.2/.gitignore000066400000000000000000000000641465632206400200560ustar00rootroot00000000000000build/ dist/ *.egg-info/ __pycache__/ .*.swp *.pyc pycoolmasternet-async-0.2.2/LICENSE000066400000000000000000000021241465632206400170720ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2020 On Freund Copyright (c) 2019 Steven Grimm Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pycoolmasternet-async-0.2.2/MANIFEST.in000066400000000000000000000000311465632206400176160ustar00rootroot00000000000000include README.md LICENSEpycoolmasternet-async-0.2.2/README.md000066400000000000000000000027451465632206400173550ustar00rootroot00000000000000# pycoolmasternet-async A Python 3 library for interacting with a [CoolMasterNet](https://coolautomation.com/products/coolmasternet/) HVAC bridge. This is a fork of [pycoolmaster](https://github.com/koreth/pycoolmasternet), modified to present an async interface and some other small changes. ## Installation You can install pycoolmasternet-async from [PyPI](https://pypi.org/project/pycoolmasternet-async/): pip3 install pycoolmasternet-async Python 3.7 and above are supported. ## How to use ```python from pycoolmasternet_async import CoolMasterNet cool = CoolMasterNet("coolmaster") # Supply the IP address and optional port number (default 10102). cool = CoolMasterNet("192.168.0.123", port=12345, read_timeout=1) # General information info = await cool.info() # Returns a dict of CoolMasterNetUnit objects. Keys are the unit IDs units = await cool.status() unit = units["L1.001"] unit.unit_id # Temperature unit: Imperial, Celsius unit.temperature_unit # Current reading of unit's thermometer unit.temperature # Current setting of unit's thermostat unit.thermostat # Setters return a new instance with updated info unit = await unit.set_thermostat(28) # True if unit is turned on unit.is_on unit = await unit.turn_on() unit = await unit.turn_off() # Fan speed: low, med, high, auto unit.fan_speed unit = await unit.set_fan_speed('med') # Mode of operation: auto, cool, dry, fan, heat unit.mode unit = await unit.set_mode('cool') # Get fresh info unit = await unit.refresh() ```pycoolmasternet-async-0.2.2/pycoolmasternet_async/000077500000000000000000000000001465632206400225135ustar00rootroot00000000000000pycoolmasternet-async-0.2.2/pycoolmasternet_async/__init__.py000066400000000000000000000001171465632206400246230ustar00rootroot00000000000000from .coolmasternet import CoolMasterNet from .coolmasternet import SWING_MODESpycoolmasternet-async-0.2.2/pycoolmasternet_async/coolmasternet.py000066400000000000000000000200121465632206400257370ustar00rootroot00000000000000import asyncio import re _MODES = ["auto", "cool", "dry", "fan", "heat"] _SWING_CHAR_TO_NAME = { "a": "auto", "h": "horizontal", "3": "30", "4": "45", "6": "60", "v": "vertical", "x": "stop", } _SWING_NAME_TO_CHAR = {value: key for key, value in _SWING_CHAR_TO_NAME.items()} SWING_MODES = list(_SWING_CHAR_TO_NAME.values()) class CoolMasterNet(): """A connection to a coolmasternet bridge.""" def __init__(self, host, port=10102, read_timeout=1, swing_support=False): """Initialize this CoolMasterNet instance to connect to a particular host at a particular port.""" self._host = host self._port = port self._read_timeout = read_timeout self._swing_support = swing_support self._status_cmd = None self._concurrent_reads = asyncio.Semaphore(3) async def _make_request(self, request): """Send a request to the CoolMasterNet and returns the response.""" async with self._concurrent_reads: reader, writer = await asyncio.open_connection(self._host, self._port) try: writer.write((request + "\n").encode("ascii")) response = await asyncio.wait_for(reader.readuntil(b"\n>"), self._read_timeout) data = response.decode("ascii") if data.startswith(">"): data = data[1:] if data.endswith("\n>"): data = data[:-1] if data.endswith("OK\r\n"): data = data[:-4] if "Unknown command" in data: cmd = request.split(" ", 1)[0] raise ValueError(f"Command '{cmd}' is not supported") return data finally: writer.close() await writer.wait_closed() async def info(self): """Get the general info the this CoolMasterNet.""" raw = await self._make_request("set") lines = raw.strip().split("\r\n") key_values = [re.split(r"\s*:\s*", line, 1) for line in lines] return dict(key_values) async def _status(self, status_cmd=None, unit_id=None): """Fetch the status of all units or a single one, falls back to legacy format if necessary""" if status_cmd is None: cmds = ['ls2', 'stat2'] else: cmds = [status_cmd] for cmd in cmds: try: final_cmd = f"{cmd} {unit_id}" if unit_id is not None else cmd status_lines = (await self._make_request(final_cmd)).strip().split("\r\n") break except ValueError: continue else: raise Exception("failed to execute status commands") return cmd, status_lines async def status(self): """Return a list of CoolMasterNetUnit objects with current status.""" self._status_cmd, status_lines = await self._status(self._status_cmd) return { key: unit for unit, key in await asyncio.gather( *(CoolMasterNetUnit.create( self, line.split(" ", 1)[0], line, self._status_cmd) for line in status_lines ) ) } class CoolMasterNetUnit(): """An immutable snapshot of a unit.""" def __init__(self, bridge, unit_id, raw, swing_raw, status_cmd): """Initialize a unit snapshot.""" self._raw = raw self._status_cmd = status_cmd self._swing_raw = swing_raw self._unit_id = unit_id self._bridge = bridge self._parse() @classmethod async def create(cls, bridge, unit_id, raw=None, status_cmd=None): if raw is None or status_cmd is None: status_cmd, status_lines = await bridge._status(status_cmd, unit_id) raw = status_lines[0] swing_raw = ((await bridge._make_request(f"query {unit_id} s")).strip() if bridge._swing_support else "") return CoolMasterNetUnit(bridge, unit_id, raw, swing_raw, status_cmd), unit_id def _parse(self): fields = re.split(r"\s+", self._raw.strip()) if len(fields) not in (8, 9): raise ConnectionError("Unexpected status line format: " + str(fields)) self._is_on = fields[1] == "ON" self._temperature_unit = "imperial" if fields[2][-1] == "F" else "celsius" self._thermostat = float(fields[2][:-1]) self._temperature = float(fields[3][:-1].replace(",", ".")) self._fan_speed = fields[4].lower() self._mode = fields[5].lower() self._error_code = fields[6] if fields[6] != "OK" else None self._clean_filter = fields[7] in ("#", "1") self._swing = _SWING_CHAR_TO_NAME.get(self._swing_raw) async def _make_unit_request(self, request): return await self._bridge._make_request(request.replace("UID", self._unit_id)) async def refresh(self): """Refresh the data from CoolMasterNet and return it as a new instance.""" return (await CoolMasterNetUnit.create(self._bridge, self._unit_id))[0] @property def unit_id(self): """The unit id.""" return self._unit_id @property def is_on(self): """Is the unit on.""" return self._is_on @property def thermostat(self): """The target temperature.""" return self._thermostat @property def temperature(self): """The current temperature.""" return self._temperature @property def fan_speed(self): """The fan spped.""" return self._fan_speed @property def mode(self): """The current mode (e.g. heat, cool).""" return self._mode @property def error_code(self): """Error code on error, otherwise None.""" return self._error_code @property def clean_filter(self): """True when the air filter needs to be cleaned.""" return self._clean_filter @property def swing(self): """The current swing mode (e.g. horizontal).""" return self._swing @property def temperature_unit(self): return self._temperature_unit async def set_fan_speed(self, value): """Set the fan speed.""" await self._make_unit_request(f"fspeed UID {value}") return await self.refresh() async def set_mode(self, value): """Set the mode.""" if not value in _MODES: raise ValueError( f"Unrecognized mode {value}. Valid values: {' '.join(_MODES)}" ) await self._make_unit_request(value + " UID") return await self.refresh() async def set_thermostat(self, value): """Set the target temperature.""" rounded = round(value, 1) await self._make_unit_request(f"temp UID {rounded}") return await self.refresh() async def set_swing(self, value): """Set the swing mode.""" if not value in SWING_MODES: raise ValueError( f"Unrecognized swing mode {value}. Valid values: {', '.join(SWING_MODES)}" ) return_value = await self._make_unit_request(f"swing UID {_SWING_NAME_TO_CHAR[value]}") if return_value.startswith("Unsupported Feature"): raise ValueError( f"Unit {self._unit_id} doesn't support swing mode {value}." ) return await self.refresh() async def turn_on(self): """Turn a unit on.""" await self._make_unit_request("on UID") return await self.refresh() async def turn_off(self): """Turn a unit off.""" await self._make_unit_request("off UID") return await self.refresh() async def reset_filter(self): """Report that the air filter was cleaned and reset the timer.""" await self._make_unit_request(f"filt UID") return await self.refresh() async def feed(self, value): """Provides ambient temperature hint to the unit.""" rounded = round(value, 1) await self._make_unit_request(f"feed UID {rounded}") pycoolmasternet-async-0.2.2/setup.py000066400000000000000000000065311465632206400176050ustar00rootroot00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- # Note: To use the 'upload' functionality of this file, you must: # $ pipenv install twine --dev import io import os import sys from shutil import rmtree from setuptools import find_packages, setup, Command # Package meta-data. NAME = 'pycoolmasternet-async' DESCRIPTION = 'A python library to control CoolMasterNet HVAC bridges over asyncio.' URL = 'https://github.com/OnFreund/pycoolmasternet-async' EMAIL = 'onfreund@gmail.com' AUTHOR = 'On Freund' REQUIRES_PYTHON = '>=3.7.0' VERSION = '0.2.2' REQUIRED = [] EXTRAS = {} here = os.path.abspath(os.path.dirname(__file__)) # Import the README and use it as the long-description. # Note: this will only work if 'README.md' is present in your MANIFEST.in file! try: with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: long_description = '\n' + f.read() except FileNotFoundError: long_description = DESCRIPTION # Load the package's __version__.py module as a dictionary. about = {} if not VERSION: project_slug = NAME.lower().replace("-", "_").replace(" ", "_") with open(os.path.join(here, project_slug, '__version__.py')) as f: exec(f.read(), about) else: about['__version__'] = VERSION class UploadCommand(Command): """Support setup.py upload.""" description = 'Build and publish the package.' user_options = [] @staticmethod def status(s): """Prints things in bold.""" print('\033[1m{0}\033[0m'.format(s)) def initialize_options(self): pass def finalize_options(self): pass def run(self): try: self.status('Removing previous builds…') rmtree(os.path.join(here, 'dist')) except OSError: pass self.status('Building Source and Wheel distribution…') os.system('{0} setup.py sdist bdist_wheel'.format(sys.executable)) self.status('Uploading the package to PyPI via Twine…') os.system('twine upload dist/*') self.status('Pushing git tags…') os.system('git tag v{0}'.format(about['__version__'])) os.system('git push --tags') sys.exit() # Where the magic happens: setup( name=NAME, version=about['__version__'], description=DESCRIPTION, long_description=long_description, long_description_content_type='text/markdown', author=AUTHOR, author_email=EMAIL, python_requires=REQUIRES_PYTHON, url=URL, packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]), # If your package is a single module, use this instead of 'packages': # py_modules=['mypackage'], # entry_points={ # 'console_scripts': ['mycli=mymodule:cli'], # }, install_requires=REQUIRED, extras_require=EXTRAS, include_package_data=True, license='MIT', classifiers=[ # Trove classifiers # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy' ], # $ setup.py publish support. cmdclass={ 'upload': UploadCommand, }, )