pax_global_header00006660000000000000000000000064141477517450014531gustar00rootroot0000000000000052 comment=6290ffaf4d8ed658dc1146eae73c8b4fdcf5a08f mampfes-python-wiffi-0c4f27b/000077500000000000000000000000001414775174500162135ustar00rootroot00000000000000mampfes-python-wiffi-0c4f27b/.gitignore000066400000000000000000000000431414775174500202000ustar00rootroot00000000000000*.swp build/ dist/ wiffi.egg-info/ mampfes-python-wiffi-0c4f27b/LICENSE000066400000000000000000000020631414775174500172210ustar00rootroot00000000000000MIT License Copyright (c) 2020 Steffen Zimmermann 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. mampfes-python-wiffi-0c4f27b/README.md000066400000000000000000000013521414775174500174730ustar00rootroot00000000000000# wiffi Python 3 package to interface devices from [STALL WIFFI](https://stall.biz). ## Installation pip3 install wiffi ## Configure the WIFFI device 1. Set "CCU-IP Adresse myCCUIP" to the IP address of Home Assistant. 2. Set port for JSON telegrams to configured server port using parameter "send_json". ## Usage ```python class WiffiIntegrationApi: def __init__(self, hass, config_entry): self._server = WiffiTcpServer(8189, self) async def __call__(self, device, metrics): # device is of type WiffiDevice print(f"mac address = {device.mac_address}") for metric in metrics: # metric is of type WiffiMetric print(f"value = {metric.value} {metric.unit_of_measurement") ``` mampfes-python-wiffi-0c4f27b/setup.py000066400000000000000000000012671414775174500177330ustar00rootroot00000000000000import setuptools with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name="wiffi", version="1.1.0", author="mampfes", author_email="mampfes@users.noreply.github.com", description="Python 3 package to interface devices from STALL WIFFI.", license="MIT", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/mampfes/python-wiffi", packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], python_requires=">=3.6", ) mampfes-python-wiffi-0c4f27b/wiffi/000077500000000000000000000000001414775174500173175ustar00rootroot00000000000000mampfes-python-wiffi-0c4f27b/wiffi/__init__.py000066400000000000000000000204651414775174500214370ustar00rootroot00000000000000"""Parser for wiffi telegrams and server for wiffi devices.""" import asyncio import json class WiffiMetric: """Representation of wiffi metric reported in json telegram.""" def __init__(self, data): """Initialize the instance.""" self._id = int(data["name"]) self._name = data["homematic_name"] self._metric_type = data["type"] self._description = data["desc"] self._unit_of_measurement = data["unit"] self._value = convert_value(data) @property def id(self): """Return integer based metric id. Called 'name' in the json telegram. """ return int(self._id) @property def name(self): """Return metric name. Called 'homematic_name' in the json telegram. """ return self._name @property def is_number(self): """Return true if the metric value type is a float number.""" return self._metric_type == "number" @property def is_bool(self): """Return true if the metric value type is boolean.""" return self._metric_type == "boolean" @property def is_string(self): """Return true if the metric value type is a string.""" return self._metric_type == "string" @property def description(self): """Return metric description.""" return self._description @property def unit_of_measurement(self): """Return metric unit of measurement. Returns an empty string for boolean and string metrics. """ return self._unit_of_measurement @property def value(self): """Return the metric value. The returned value is either a float, bool or string depending on the metric type. """ return self._value def convert_value(var): """Convert the metric value from string into python type.""" if var["type"] == "number": return float(var["value"]) if var["type"] == "boolean": return var["value"] == "true" if var["type"] == "string": return var["value"] print("can't convert unknown type {} for var {}".format(var["type"], var["name"])) return None class WiffiMetricFromSystemInfo: """Representation of wiffi metric reported in json telegram.""" def __init__(self, name, uom, metric_type, value): """Initialize the instance.""" self._name = name self._metric_type = metric_type self._unit_of_measurement = uom if metric_type == "number": self._value = float(value) elif metric_type == "bool": self._value = value == "true" else: self._value = value @property def id(self): """Return integer based metric id.""" return hash(self._name) @property def name(self): """Return metric name.""" return self._name @property def is_number(self): """Return true if the metric value type is a float number.""" return self._metric_type == "number" @property def is_bool(self): """Return true if the metric value type is boolean.""" return self._metric_type == "boolean" @property def is_string(self): """Return true if the metric value type is a string.""" return self._metric_type == "string" @property def description(self): """Return metric description.""" return self._name @property def unit_of_measurement(self): """Return metric unit of measurement. Returns an empty string for boolean and string metrics. """ return self._unit_of_measurement @property def value(self): """Return the metric value. The returned value is either a float, bool or string depending on the metric type. """ return self._value class WiffiDevice: """Representation of wiffi device properties reported in the json telegram.""" def __init__(self, moduletype, data, configuration_url): """Initialize the instance.""" self._moduletype = moduletype self._mac_address = data["MAC-Adresse"] self._dest_ip = data["Homematic_CCU_ip"] self._wlan_ssid = data["WLAN_ssid"] self._wlan_signal_strength = float(data["WLAN_Signal_dBm"]) self._sw_version = data["firmware"] self._configuration_url = configuration_url @property def moduletype(self): """Return the wiffi device type, e.g. 'weatherman'.""" return self._moduletype @property def mac_address(self): """Return the mac address of the wiffi device.""" return self._mac_address @property def dest_ip(self): """Return the destination ip address for json telegrams.""" return self._dest_ip @property def wlan_ssid(self): """Return the configured WLAN ssid.""" return self._wlan_ssid @property def wlan_signal_strength(self): """Return the measured WLAN signal strength in dBm.""" return self._wlan_signal_strength @property def sw_version(self): """Return the firmware revision string of the wiffi device.""" return self._sw_version @property def configuration_url(self): """Return the URL to the web interface of the wiffi device.""" return self._configuration_url class WiffiConnection: """Representation of a TCP connection between a wiffi device and the server. The following behaviour has been observed with weatherman firmware 107: For every json telegram which has to be sent by the wiffi device to the TCP server, a new TCP connection will be opened. After 1 json telegram has been transmitted, the connection will be closed again. The telegram is terminated by a 0x04 character. Therefore we read until we receive a 0x04 character and parse the whole telegram afterwards. Then we wait for the next telegram, but the connection will be closed by the wiffi device. Therefore we get an 'IncompleteReadError exception which we will ignore. We don't close the connection on our own, because the behaviour that the wiffi device closes the connection after every telegram may change in the future. """ def __init__(self, server): """Initialize the instance.""" self._server = server async def __call__(self, reader, writer): """Process callback from the TCP server if a new connection has been opened.""" peername = writer.get_extra_info("peername") while not reader.at_eof(): try: data = await reader.readuntil( b"\x04" ) # read until separator \x04 received await self.parse_msg(peername, data[:-1]) # omit separator with [:-1] except asyncio.IncompleteReadError: pass async def parse_msg(self, peername, raw_data): """Parse received telegram which is terminated by 0x04.""" data = json.loads(raw_data.decode("utf-8")) moduletype = data["modultyp"] systeminfo = data["Systeminfo"] configuration_url = f"http://{peername[0]}" metrics = [] for var in data["vars"]: metrics.append(WiffiMetric(var)) metrics.append(WiffiMetricFromSystemInfo("rssi", "dBm", "number", data["Systeminfo"]["WLAN_Signal_dBm"])) metrics.append(WiffiMetricFromSystemInfo("uptime", "s", "number", data["Systeminfo"]["sec_seit_reset"])) metrics.append(WiffiMetricFromSystemInfo("ssid", None, "string", data["Systeminfo"]["WLAN_ssid"])) if self._server.callback is not None: await self._server.callback(WiffiDevice(moduletype, systeminfo, configuration_url), metrics) class WiffiTcpServer: """Manages TCP server for wiffi devices. Opens a single port and listens for incoming TCP connections. """ def __init__(self, port, callback=None): """Initialize instance.""" self.port = port self.callback = callback self.server = None async def start_server(self): """Start TCP server on configured port.""" self.server = await asyncio.start_server(WiffiConnection(self), port=self.port) async def close_server(self): """Close TCP server.""" if self.server is not None: self.server.close() await self.server.wait_closed()