Skip to content

Commit

Permalink
Merge pull request #53 from nobbi1991/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
nobbi1991 authored Apr 1, 2024
2 parents 04d6e71 + 3ef9a3f commit 2dcac11
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Create Docker Image
on:
release:
types: [published]
workflow_dispatch:

jobs:
buildx:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion Manifest.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include habapp_rules/energy/monthly_report_template.html
include resources/*
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion habapp_rules/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Set version for the package."""
__version__ = "5.6.0"
__version__ = "5.6.1"
13 changes: 8 additions & 5 deletions habapp_rules/energy/monthly_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,14 +111,16 @@ 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)
:param known_energy_share: list of EnergyShare objects
: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)
Expand All @@ -136,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:
Expand Down Expand Up @@ -188,10 +194,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(),
Expand Down
Empty file added resources/__init__.py
Empty file.
Empty file added resources/energy/__init__.py
Empty file.
Empty file added tests/energy/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/energy/monthly_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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, "[email protected]")

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, "[email protected]", True)

def test_get_historic_value(self):
"""Test _get_historic_value."""
mock_item = unittest.mock.MagicMock()
Expand Down

0 comments on commit 2dcac11

Please sign in to comment.