From 62c0ac1deab1a20d7912d9d9adda1495a98e215b Mon Sep 17 00:00:00 2001 From: msinn Date: Mon, 31 Jul 2023 14:08:55 +0200 Subject: [PATCH] Initial version of lib.env --- doc/user/source/referenz/APIs.rst | 20 +- doc/user/source/release/1_01_and_newer.rst | 17 ++ .../source/release/1_01_bis_1_05_releases.rst | 13 - doc/user/source/release/1_10.rst | 16 +- doc/user/source/release/release.rst | 2 - .../source/release/smarthome_py/releases.rst | 14 -- .../release/smarthomeng_older/releases.rst | 12 - doc/user/source/was_ist_neu.rst | 4 +- lib/env/__init__.py | 30 +++ lib/env/envlib.py | 230 ++++++++++++++++++ 10 files changed, 307 insertions(+), 51 deletions(-) create mode 100644 doc/user/source/release/1_01_and_newer.rst delete mode 100644 doc/user/source/release/1_01_bis_1_05_releases.rst delete mode 100644 doc/user/source/release/smarthome_py/releases.rst delete mode 100644 doc/user/source/release/smarthomeng_older/releases.rst create mode 100644 lib/env/__init__.py create mode 100644 lib/env/envlib.py diff --git a/doc/user/source/referenz/APIs.rst b/doc/user/source/referenz/APIs.rst index 9dee7d325..ed9a0bc6a 100644 --- a/doc/user/source/referenz/APIs.rst +++ b/doc/user/source/referenz/APIs.rst @@ -14,15 +14,25 @@ Die Beschreibung ihrer Funktionalität ist hier beschrieben: :maxdepth: 3 :titlesonly: - /lib/item - /lib/plugin + /lib/item + /lib/plugin .. toctree:: :maxdepth: 3 :titlesonly: - /lib/shtime - /lib/logic - /lib/scheduler + /lib/shtime + /lib/logic + /lib/scheduler + +Folgende Libraries mit Hilfsfunktionen stehen zur Verfügung: + +.. toctree:: + :maxdepth: 3 + :titlesonly: + + /lib/env + /lib/tools + /lib/utils diff --git a/doc/user/source/release/1_01_and_newer.rst b/doc/user/source/release/1_01_and_newer.rst new file mode 100644 index 000000000..e3d3461b2 --- /dev/null +++ b/doc/user/source/release/1_01_and_newer.rst @@ -0,0 +1,17 @@ +:tocdepth: 2 + +Release 1.1 bis 1.5 +=================== + +.. toctree:: + :maxdepth: 0 + :titlesonly: + + smarthomeng_older/1_05_1 + smarthomeng_older/1_05 + smarthomeng_older/1_04_2 + smarthomeng_older/1_04_1 + smarthomeng_older/1_04 + smarthomeng_older/1_03 + smarthomeng_older/1_02 + smarthomeng_older/1_01 diff --git a/doc/user/source/release/1_01_bis_1_05_releases.rst b/doc/user/source/release/1_01_bis_1_05_releases.rst deleted file mode 100644 index 86e64d104..000000000 --- a/doc/user/source/release/1_01_bis_1_05_releases.rst +++ /dev/null @@ -1,13 +0,0 @@ -:tocdepth: 3 - -Release 1.1 bis 1.5 -=================== - -x - -.. toctree:: - :maxdepth: 1 - :titlesonly: - :reversed: - - smarthomeng_older/* diff --git a/doc/user/source/release/1_10.rst b/doc/user/source/release/1_10.rst index 336295ea9..f421b32bf 100644 --- a/doc/user/source/release/1_10.rst +++ b/doc/user/source/release/1_10.rst @@ -17,10 +17,10 @@ sowie einige neue Plugins. Diese Release Notes sind ein Arbeitsstand. - - Berücksichtigt sind Commits im smarthome Repository bis incl. 27. Juli 2023 - (lib.shtime: Changed a loglevel from notice to info...) - - Berücksichtigt sind Commits im plugins Repository bis incl. 27. Juli 2023 - (Merge pull request #765 from sisamiwe/...) + - Berücksichtigt sind Commits im smarthome Repository bis incl. 31. Juli 2023 + (modules.admin: Bugfix in api_logics) + - Berücksichtigt sind Commits im plugins Repository bis incl. 28. Juli 2023 + (...) Überblick @@ -122,6 +122,7 @@ Updates im CORE * lib.network: * Fix name reference + * Small fixes * lib.plugin: @@ -173,6 +174,7 @@ Updates im CORE * Added description attribute to item details * In Logger configuration sub-loggers of a plugin are en-/disabled with the plugin logger * v0.8.17 Implemented load logics from editor page after it has been unloaded due to error + * v0.9.1: Adding, changing and deleting of logic-groups implemented * http: @@ -193,6 +195,7 @@ Updates im CORE * Add sorting icons for num values * Update documentation.. add info on tooltips, datepicker, cookies, icons * Update sample plugin to include cookies and tooltips + * Updates to datatables and pageresize * websocket: @@ -375,6 +378,11 @@ http://www.smarthomeng.de/user/plugins_all.html konsultieren. * Changed default value for generate_pages to False * Corrected log message + * only check dir if handling option is set + +* solarforecast: + + * Fallback to smarthomeNG lat/long default values if no plugin specific latitude and longitude values are specified * stateengine: diff --git a/doc/user/source/release/release.rst b/doc/user/source/release/release.rst index 12a90a9b9..24a842ef9 100644 --- a/doc/user/source/release/release.rst +++ b/doc/user/source/release/release.rst @@ -12,6 +12,4 @@ Release Notes für SmartHomeNG: :reversed: * - smarthomeng_older/releases - smarthome_py/releases diff --git a/doc/user/source/release/smarthome_py/releases.rst b/doc/user/source/release/smarthome_py/releases.rst deleted file mode 100644 index 1f1eeed1d..000000000 --- a/doc/user/source/release/smarthome_py/releases.rst +++ /dev/null @@ -1,14 +0,0 @@ -:tocdepth: 2 - -Release 1.0 und davor -===================== - - -Die folgenden Release Notes beziehen sich auf den Vorläufer **smarthome.py**: - -.. toctree:: - :maxdepth: 1 - :titlesonly: - :reversed: - - * diff --git a/doc/user/source/release/smarthomeng_older/releases.rst b/doc/user/source/release/smarthomeng_older/releases.rst deleted file mode 100644 index 4d9bf2869..000000000 --- a/doc/user/source/release/smarthomeng_older/releases.rst +++ /dev/null @@ -1,12 +0,0 @@ -:tocdepth: 2 - -Release 1.1 bis 1.5 -=================== - - -.. toctree:: - :maxdepth: 1 - :titlesonly: - :reversed: - - * diff --git a/doc/user/source/was_ist_neu.rst b/doc/user/source/was_ist_neu.rst index f7d45ebbc..34dd3f572 100644 --- a/doc/user/source/was_ist_neu.rst +++ b/doc/user/source/was_ist_neu.rst @@ -27,8 +27,10 @@ diesem und den vorangegangenen Releases ist den :doc:`Release Notes . +######################################################################### + + +""" +This library implements functions to handle environmental data in SmartHomeNG. +""" + +from .envlib import * + + + diff --git a/lib/env/envlib.py b/lib/env/envlib.py new file mode 100644 index 000000000..3783edb1a --- /dev/null +++ b/lib/env/envlib.py @@ -0,0 +1,230 @@ +#!/usr/bin/env python3 +# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab +######################################################################### +# Copyright 2023- Martin Sinn m.sinn@gmx.de +######################################################################### +# This file is part of SmartHomeNG. +# +# SmartHomeNG is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SmartHomeNG is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with SmartHomeNG. If not, see . +######################################################################### + + +class Env: + + _sh = None + _logger = None + + # source for german descriptions https://www.smarthomeng.de/vom-winde-verweht + _beaufort_descriptions_de = ["Windstille", + "leiser Zug", + "leichte Brise", + "schwacher Wind", + "mäßiger Wind", + "frischer Wind", + "starker Wind", + "steifer Wind", + "stürmischer Wind", + "Sturm", + "schwerer Sturm", + "orkanartiger Sturm", + "Orkan"] + # source for english descriptions https://simple.wikipedia.org/wiki/Beaufort_scale + _beaufort_descriptions_en = ["Calm", + "Light air", + "Light breeze", + "Gentle breeze", + "Moderate breeze", + "Fresh breeze", + "Strong breeze", + "High wind", + "Fresh Gale", + "Strong Gale", + "Storm", + "Violent storm", + "Hurricane-force"] + + + + def __init__(self, smarthome): + + self._sh = smarthome + self._logger = smarthome._logger + + + def mps_to_beaufort(self, speed_in_mps): + """ + Convert wind speed from meters per second to beaufort + + :param speed_in_mps: wind speed in miles per second + :return: Wind speed in beauford (Bft) - range from 0 - 12 + """ + try: + # Origin of table: https://www.smarthomeng.de/vom-winde-verweht + table = [ + (0.3, 0), + (1.6, 1), + (3.4, 2), + (5.5, 3), + (8.0, 4), + (10.8, 5), + (13.9, 6), + (17.2, 7), + (20.8, 8), + (24.5, 9), + (28.5, 10), + (32.7, 11), + (999, 12)] + return min(filter(lambda x: x[0] >= speed_in_mps, table))[1] + except ValueError: + self._logger.error(f"Cannot translate wind-speed to beaufort-number, received: '{speed_in_mps}'") + return None + + + @staticmethod + def kmh_to_beaufort(speed_in_kmh): + """ + Convert wind speed from kilometers per hour to beaufort + + :param speed_in_kmh: wind speed in kilometers per hour + :return: Wind speed in beauford (Bft) - range from 0 - 12 + """ + + return mps_to_beaufort(kmh_to_mps(speed_in_kmh)) + + + def get_beaufort_description(self, speed_in_bft, sh=None): + """ + Get description for windspeed in beaufort + + :param speed_in_bft: Wind speed in beauford (Bft) - range from 0 - 12 + :param sh: + :return: + """ + if speed_in_bft is None: + self._logger.warning(f"speed_in_bft is given as None") + return None + if type(speed_in_bft) is not int: + self._logger.error( + f"speed_in_bft is not given as int: '{speed_in_bft}'") + return None + if (speed_in_bft < 0) or (speed_in_bft > 12): + self._logger.error( + f"speed_in_bft is out of scale: '{speed_in_bft}'") + return None + + if sh is None or sh.get_defaultlanguage() == 'de': + return self._beaufort_descriptions_de[speed_in_bft] + return self._beaufort_descriptions_en[speed_in_bft] + + + @staticmethod + def mps_to_kmh(speed_in_mps): + """ + Umterchnung m/s in km/h + + :param speed_in_mps: + :return: + """ + return speed_in_mps * 3.6 + + + @staticmethod + def kmh_to_mps(speed_in_kmh): + """ + Umterchnung km/h in m/s + + :param speed_in_mps: + :return: + """ + return speed_in_kmh / 3.6 + + + @staticmethod + def kn_to_kmh(speed_in_kn): + """ + Umrechnung Knoten (nm/h) in km/h + + :param speed_in_kn: + :return: + """ + return speed_in_kn * 1.852 + + + @staticmethod + def kmh_to_kn(speed_in_kmh): + """ + Umrechnung km/h in Knoten (nm/h) + + :param speed_in_kmh: + :return: + """ + return speed_in_kmh / 1.852 + + + + + @staticmethod + def get_wind_direction8(deg): + + direction_array = ['N', 'NO', 'O', 'SO', 'S', 'SW', 'W', 'NW', 'N'] + + index = int( (deg % 360 + 22.5) / 45) + return direction_array[index] + + + @staticmethod + def get_wind_direction16(deg): + + direction_array = ['N', 'NNO', 'NO', 'ONO', 'O', 'OSO', 'SO', 'SSO', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'] + + index = int( (deg % 360 + 11.25) / 22.5) + return direction_array[index] + + + + def get_location_name(self, lat=None, lon=None): + + import requests + + if lat is None: + lat = self._sh._lat + if lon is None: + lon = self._sh._lon + + if lat == 0 or lon == 0: + self._logger.debug(f"lat or lon are zero, not sending request: {lat=}, {lon=}") + return + + # api documentation: https://nominatim.org/release-docs/develop/api/Reverse/ + request_str = f"https://nominatim.openstreetmap.org/reverse?lat={lat}&lon={lon}&format=jsonv2" + + try: + response = requests.get(request_str) + except Exception as e: + self._logger.warning(f"get_location_name: Exception when sending GET request: {e}") + return + + try: + json_obj = response.json() + except Exception as e: + self._logger.warning(f"get_location_name: Response '{response}' is no valid json format: {e}") + return '' + + if response.status_code >= 500: + self._logger.warning(f"get_location_name: {self.get_location_name(response.status_code)}") + return '' + + #self._logger.notice(f"{json_obj['display_name']}") + #self._logger.notice(f"{json_obj['address']}") + return json_obj['address']['suburb']