Skip to content

Commit

Permalink
Merge pull request #124 from nobbi1991/fix_ventilation
Browse files Browse the repository at this point in the history
fixed bug in ventilation for long_absence
  • Loading branch information
nobbi1991 authored Dec 31, 2024
2 parents 59fb5bf + 1e70307 commit e6d81d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Version 7.2.1 - dd.12.2024
# Version 7.2.1 - 31.12.2024

## Bugfix

- ...
- Added workaround for all rules of `habapp_rules.actors.ventilation` for triggering the ventilation if presence state is `long_absence`. Check the following GitHub link for more details: https://github.com/spacemanspiff2007/eascheduler/issues/24

# Version 7.2.0 - 15.12.2024

Expand Down
21 changes: 20 additions & 1 deletion habapp_rules/actors/ventilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
LEVEL_POWER = 2


def _to_datetime(time_input: datetime.time) -> datetime.datetime: # this is needed because of https://github.com/spacemanspiff2007/eascheduler/issues
"""Converts a datetime.time object to a datetime.datetime object using today's date. Adds one day if the time is in the past.
Args:
time_input: The time to convert.
Returns:
The resulting datetime object with today's date or the next day if the time is in the past.
"""
result = datetime.datetime.combine(datetime.datetime.now(), time_input)

# If the resulting datetime is in the past, add one day
if result <= datetime.datetime.now():
result += datetime.timedelta(days=1)

return result


class _VentilationBase(habapp_rules.core.state_machine_rule.StateMachineRule):
"""Class for ventilation objects."""

Expand Down Expand Up @@ -207,7 +225,8 @@ def on_enter_Auto_Init(self) -> None: # noqa: N802

def on_enter_Auto_LongAbsence_Off(self) -> None: # noqa: N802
"""Is called on entering of Auto_LongAbsence_Off state."""
self.run.once(self._config.parameter.state_long_absence.start_time, self._trigger_long_absence_power_on)
trigger_time = _to_datetime(self._config.parameter.state_long_absence.start_time)
self.run.once(trigger_time, self._trigger_long_absence_power_on)

def _trigger_long_absence_power_on(self) -> None:
"""Trigger long absence power on."""
Expand Down
29 changes: 27 additions & 2 deletions tests/actors/ventilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@
import tests.helper.timer


class TestGlobalFunctions(unittest.TestCase):
"""Test global functions."""

def test_to_datetime(self) -> None:
"""Test _to_datetime."""
TestCase = collections.namedtuple("TestCase", "input_time, now, expected_result")

test_cases = [
TestCase(datetime.time(0, 0), datetime.datetime(2024, 1, 1, 0, 1), datetime.datetime(2024, 1, 2, 0, 0)),
TestCase(datetime.time(6, 0), datetime.datetime(2024, 1, 1, 0, 1), datetime.datetime(2024, 1, 1, 6, 0)),
TestCase(datetime.time(18, 0), datetime.datetime(2024, 1, 1, 17, 59), datetime.datetime(2024, 1, 1, 18, 0)),
TestCase(datetime.time(18, 0), datetime.datetime(2024, 1, 1, 18, 00), datetime.datetime(2024, 1, 2, 18, 0)),
TestCase(datetime.time(18, 0), datetime.datetime(2024, 1, 1, 18, 1), datetime.datetime(2024, 1, 2, 18, 0)),
]

combine_orig = datetime.datetime.combine

with unittest.mock.patch("datetime.datetime") as datetime_mock:
datetime.datetime.combine = combine_orig
for test_case in test_cases:
with self.subTest(test_case=test_case):
datetime_mock.now.return_value = test_case.now
self.assertEqual(test_case.expected_result, habapp_rules.actors.ventilation._to_datetime(test_case.input_time))


class TestVentilation(tests.helper.test_case_base.TestCaseBaseStateMachine):
"""Tests cases for testing Ventilation."""

Expand Down Expand Up @@ -223,9 +248,9 @@ def test_set_feedback_states(self) -> None:

def test_on_enter_long_absence_off(self) -> None:
"""Test on_enter_Auto_LongAbsence_Off."""
with unittest.mock.patch.object(self.ventilation_max, "_trigger_long_absence_power_on") as trigger_on_mock:
with unittest.mock.patch.object(self.ventilation_max, "_trigger_long_absence_power_on") as trigger_on_mock, unittest.mock.patch("habapp_rules.actors.ventilation._to_datetime") as to_datetime_mock:
self.ventilation_max.to_Auto_LongAbsence_Off()
self.run_at_mock.assert_called_once_with(datetime.time(18), trigger_on_mock)
self.run_at_mock.assert_called_once_with(to_datetime_mock.return_value, trigger_on_mock)

def test_trigger_long_absence_power_on(self) -> None:
"""Test _trigger_long_absence_power_on."""
Expand Down

0 comments on commit e6d81d9

Please sign in to comment.