From 54dacad94497c030d5a0d580cabd94238b2e3214 Mon Sep 17 00:00:00 2001 From: nobbi1991 <48419518+nobbi1991@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:43:31 +0200 Subject: [PATCH 1/2] fixed missing resources in if used with docker --- .github/workflows/docker-image.yml | 1 + Dockerfile | 1 + Manifest.in | 2 +- changelog.md | 6 ++++++ habapp_rules/__version__.py | 2 +- habapp_rules/energy/monthly_report.py | 6 ++---- resources/__init__.py | 0 resources/energy/__init__.py | 0 .../energy/monthly_report_template.html | 0 tests/energy/__init__.py | 0 10 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 resources/__init__.py create mode 100644 resources/energy/__init__.py rename {habapp_rules => resources}/energy/monthly_report_template.html (100%) create mode 100644 tests/energy/__init__.py diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index ddfa630..2a20916 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -3,6 +3,7 @@ name: Create Docker Image on: release: types: [published] + workflow_dispatch: jobs: buildx: diff --git a/Dockerfile b/Dockerfile index 0b67232..1276363 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ FROM python:3.11 COPY --from=buildimage /root/wheels /root/wheels COPY container/entrypoint.sh /entrypoint.sh +COPY resources/ /resources ENV HABAPP_HOME=/habapp \ USER_ID=9001 \ diff --git a/Manifest.in b/Manifest.in index b5034bd..e907b40 100644 --- a/Manifest.in +++ b/Manifest.in @@ -1 +1 @@ -include habapp_rules/energy/monthly_report_template.html \ No newline at end of file +include resources/* \ No newline at end of file diff --git a/changelog.md b/changelog.md index 89d8ac1..982e460 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +# Version 5.6.1 - 01.04.2024 + +# Bugfix + +- fixed bug of missing resources of ``habapp_rules.energy.montly_report.MonthlyReport`` if used in docker container + # Version 5.6.0 - 24.03.2024 # Features diff --git a/habapp_rules/__version__.py b/habapp_rules/__version__.py index 50cf21e..7f1a02e 100644 --- a/habapp_rules/__version__.py +++ b/habapp_rules/__version__.py @@ -1,2 +1,2 @@ """Set version for the package.""" -__version__ = "5.6.0" +__version__ = "5.6.1" diff --git a/habapp_rules/energy/monthly_report.py b/habapp_rules/energy/monthly_report.py index 778ee06..88e5a59 100644 --- a/habapp_rules/energy/monthly_report.py +++ b/habapp_rules/energy/monthly_report.py @@ -10,6 +10,7 @@ import dateutil.relativedelta import jinja2 import multi_notifier.connectors.connector_mail +import pkg_resources import habapp_rules.__version__ import habapp_rules.core.exceptions @@ -188,10 +189,7 @@ def _create_html(self, energy_sum_month: float) -> str: </body> </html> """ - html_template_path = pathlib.Path(__file__).parent / "monthly_report_template.html" - - with html_template_path.open() as html_template_file: - html_template = html_template_file.read() + html_template = pkg_resources.resource_string("resources.energy", "monthly_report_template.html").decode("utf-8") return jinja2.Template(html_template).render( month=_get_previous_month_name(), diff --git a/resources/__init__.py b/resources/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/resources/energy/__init__.py b/resources/energy/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/habapp_rules/energy/monthly_report_template.html b/resources/energy/monthly_report_template.html similarity index 100% rename from habapp_rules/energy/monthly_report_template.html rename to resources/energy/monthly_report_template.html diff --git a/tests/energy/__init__.py b/tests/energy/__init__.py new file mode 100644 index 0000000..e69de29 From 3ef9a3f962fc18312f9d1e776dba5abf6b751abb Mon Sep 17 00:00:00 2001 From: nobbi1991 <48419518+nobbi1991@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:52:52 +0200 Subject: [PATCH 2/2] added debug mode --- habapp_rules/energy/monthly_report.py | 7 ++++++- tests/energy/monthly_report.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/habapp_rules/energy/monthly_report.py b/habapp_rules/energy/monthly_report.py index 88e5a59..8d92c5b 100644 --- a/habapp_rules/energy/monthly_report.py +++ b/habapp_rules/energy/monthly_report.py @@ -111,7 +111,8 @@ def __init__( known_energy_share: list[EnergyShare], persistence_group_name: str | None, config_mail: multi_notifier.connectors.connector_mail.MailConfig | None, - recipients: str | list[str]) -> None: + recipients: str | list[str], + debug: bool = False) -> None: """Initialize the rule. :param name_energy_sum: name of OpenHAB Number item, which holds the total energy consumption (NumberItem) @@ -119,6 +120,7 @@ def __init__( :param persistence_group_name: OpenHAB group name which holds all items which are persisted. If the group name is given it will be checked if all energy items are in the group :param config_mail: config for sending mails :param recipients: list of recipients who get the mail + :param debug: if debug mode is active :raises habapp_rules.core.exceptions.HabAppRulesConfigurationException: if config is not valid """ HABApp.Rule.__init__(self) @@ -137,6 +139,9 @@ def __init__( raise habapp_rules.core.exceptions.HabAppRulesConfigurationException(f"The following OpenHAB items are not in the persistence group '{persistence_group_name}': {not_in_persistence_group}") self.run.at(next_trigger_time := _get_next_trigger(), self._cb_send_energy) + if debug: + self._instance_logger.warning("Debug mode is active!") + self.run.soon(self._cb_send_energy) self._instance_logger.info(f"Successfully initiated monthly consumption rule for {name_energy_sum}. Triggered first execution to {next_trigger_time.isoformat()}") def _get_historic_value(self, item: HABApp.openhab.items.NumberItem, start_time: datetime.datetime) -> float: diff --git a/tests/energy/monthly_report.py b/tests/energy/monthly_report.py index 046c20b..ee55a97 100644 --- a/tests/energy/monthly_report.py +++ b/tests/energy/monthly_report.py @@ -13,6 +13,7 @@ import tests.helper.oh_item import tests.helper.test_case_base + # pylint: disable=protected-access class TestFunctions(unittest.TestCase): """Test all global functions.""" @@ -135,6 +136,10 @@ def test_init(self): else: habapp_rules.energy.monthly_report.MonthlyReport("Energy_Sum", [self._energy_1, self._energy_2], "PersistenceGroup", self._mail_config, "test@test.de") + def test_init_with_debug_mode(self): + """Test init with debug mode.""" + self._rule = habapp_rules.energy.monthly_report.MonthlyReport("Energy_Sum", [self._energy_1, self._energy_2], None, self._mail_config, "test@test.de", True) + def test_get_historic_value(self): """Test _get_historic_value.""" mock_item = unittest.mock.MagicMock()