def test_mock_py_config_context_with_timers(self):
        with mock_py_config_context():
            save_timers((((1, 2), (3, 4)), ))
            assert restore_timers() == (((1, 2), (3, 4)), )

            save_timers((((5, 6), (7, 8)), ))
            assert restore_timers() == (((5, 6), (7, 8)), )
Пример #2
0
    def test_update_relay_switch_without_timers(self):
        with mock_py_config_context():
            assert restore_timers() == ()

            update_power_timer(self.context)

            print(get_info_text(self.context))
            self.assertEqual(get_current_timer(self.context),
                             (None, None, None))
    def test_py_config(self):
        with mock_py_config_context():

            # test_py_config() # will not work!

            save_py_config(module_name='test_int', value=1)
            assert restore_py_config(module_name='test_int') == 1

            save_py_config(module_name='test_dict', value={'foo': 1.2})
            assert restore_py_config(module_name='test_dict') == {'foo': 1.2}
Пример #4
0
    def test_update_relay_switch_in_1min(self):
        machine.RTC().deinit()  # start time from 1.1.2000 00:00
        with mock_py_config_context():
            save_timers((((0, 1), (1, 0)), ))
            save_active_days((0, 1, 2, 3, 4, 5, 6))

            assert machine.RTC().datetime((2000, 1, 1, 6, 0, 0, 0, 0))
            assert localtime_isoformat(sep=' ') == '2000-01-01 00:00:00'

            timers = restore_timers()
            assert pformat_timers(timers) == '00:01 - 01:00'
            assert list(iter_times(timers)) == [(True, (0, 1, 0)),
                                                (False, (1, 0, 0))]

            update_power_timer(self.context)
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 1 min at 00:01 h.')
Пример #5
0
    def test_update_relay_switch_today_not_active(self):
        machine.RTC().deinit()  # start time from 1.1.2000 00:00
        with mock_py_config_context():
            save_timers((((1, 0), (2, 0)), ))
            save_active_days((0, 1, 2, 3, 4, 6))  # <- day 5 is missing

            assert machine.RTC().datetime((2000, 1, 1, 5, 0, 0, 0, 0))
            assert localtime_isoformat(sep=' ') == '2000-01-01 00:00:00'

            update_power_timer(self.context)
            print(get_info_text(self.context))

            assert active_today() is False
            assert self.context.power_timer_today_active is False

            self.assertEqual(get_info_text(self.context),
                             'Power timer is not active today.')
    def test_mock_py_config_context(self):
        with mock_py_config_context():
            # check mock if file not exist, yet:
            with self.assertRaises(ImportError) as cm:
                __import__('not_existing_module', None, None)

            self.assertEqual(
                cm.exception.args[0],
                'File not found: not_existing_module.py')  # Own error?

            # Check mock if file exists:
            test_py_path = Path('test.py')
            try:
                with test_py_path.open('w') as f:
                    f.write('variable=True')
                test = __import__('test', None, None)
                assert test.variable is True
            finally:
                test_py_path.unlink()
    def test_get_current_timer(self):
        with mock_py_config_context():
            save_timers((
                ((0, 0), (0, 1)),
                ((0, 2), (0, 3)),
            ))
            update_power_timer(self.context)

            rtc = machine.RTC()
            rtc.datetime((2000, 1, 1, 5, 0, 0, 0, 0))  # 00:00
            assert get_current_timer(self.context) == (0, False, 60)

            rtc.datetime((2000, 1, 1, 5, 0, 1, 0, 0))  # 00:01
            assert get_current_timer(self.context) == (60, True, 120)

            rtc.datetime((2000, 1, 1, 5, 0, 2, 0, 0))  # 00:02
            assert get_current_timer(self.context) == (120, False, 180)

            rtc.datetime((2000, 1, 1, 5, 0, 3, 0, 0))  # 00:03 -> next timer is on next day!
            assert get_current_timer(self.context) == (180, True, 86400)
Пример #8
0
    def test_relay_switch_timers_and_overwrite(self):
        rtc = machine.RTC()
        with mock_py_config_context():
            save_timers((((10, 0), (20, 0)), ))
            timers = restore_timers()
            assert pformat_timers(timers) == '10:00 - 20:00'
            assert list(iter_times(timers)) == [(True, (10, 0, 0)),
                                                (False, (20, 0, 0))]

            save_active_days((0, 1, 2, 3, 4, 5, 6))

            assert get_active_days() == (0, 1, 2, 3, 4, 5, 6)

            rtc.datetime(
                (2000, 1, 2, 6, 0, 0, 0, 0))  # set RTC time: 2.1.2000 00:00:00
            assert localtime_isoformat(sep=' ') == '2000-01-02 00:00:00'

            assert_current_timer(  # Turn ON at 10:00
                '72000 -4 h 20:00 2000-01-01T20:00:00'
                ' -> 122400 10 h 10:00 2000-01-02T10:00:00 -> ON',
                self.context)

            # init relay in state 'OFF'
            Pins.relay.off()
            assert Pins.relay.state == 'OFF'

            # No manual overwrite and ON timer not yet reached -> OFF and turn ON at 10:00
            update_power_timer(self.context)
            assert active_today() is True
            assert self.context.power_timer_today_active is True
            assert Pins.relay.state == 'OFF'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 10 h at 10:00 h.')

            # 2000-01-01 09:59:59 - timer not yet reached
            rtc.datetime((2000, 1, 2, 6, 9, 59, 59, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'OFF'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 1 sec at 10:00 h.')

            # 2000-01-01 10:00:00 - turn ON by timer
            rtc.datetime((2000, 1, 2, 6, 10, 0, 0, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch off in 10 h at 20:00 h.')

            # 2000-01-01 20:00:00 - turn OFF by timer
            rtc.datetime((2000, 1, 2, 6, 20, 0, 0, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'OFF'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 14 h at 10:00 h.')

            # 2000-01-02 09:00:00 - manual overwrite
            rtc.datetime((2000, 1, 2, 6, 9, 0, 0, 0))
            update_rtc_dict({
                constants.RTC_KEY_MANUAL_OVERWRITE: utime.time(),
                constants.RTC_KEY_MANUAL_OVERWRITE_TYPE: True  # -> turn ON
            })
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 60 min at 10:00 h.')
            # 2000-01-02 09:59:59 - manual overwrite is still active
            rtc.datetime((2000, 1, 2, 6, 9, 59, 59, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 1 sec at 10:00 h.')
            # 2000-01-02 12:00:00 - Normal timer mode, still ON
            rtc.datetime((2000, 1, 2, 6, 12, 0, 0, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch off in 8 h at 20:00 h.')
            # manual overwrite
            update_rtc_dict({
                constants.RTC_KEY_MANUAL_OVERWRITE: utime.time(),
                constants.RTC_KEY_MANUAL_OVERWRITE_TYPE: False  # -> turn OFF
            })
            update_power_timer(self.context)
            assert Pins.relay.state == 'OFF'
            self.assertEqual(get_info_text(self.context),
                             'Switch off in 8 h at 20:00 h.')
            # 2000-01-02 20:00:00 - Normal timer mode -> switch OFF
            rtc.datetime((2000, 1, 2, 6, 20, 0, 0, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'OFF'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 14 h at 10:00 h.')
            # manual overwrite
            update_rtc_dict({
                constants.RTC_KEY_MANUAL_OVERWRITE: utime.time(),
                constants.RTC_KEY_MANUAL_OVERWRITE_TYPE: True  # -> turn ON
            })
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 14 h at 10:00 h.')
            # 2000-01-01 09:59:59 - timer not yet reached
            rtc.datetime((2000, 1, 2, 6, 9, 59, 59, 0))
            update_power_timer(self.context)
            assert Pins.relay.state == 'ON'
            self.assertEqual(get_info_text(self.context),
                             'Switch on in 1 sec at 10:00 h.')
 def test_timezone(self):
     with mock_py_config_context():
         run_all_timezone_tests()
 def test_times_utils(self):
     with mock_py_config_context():
         run_all_times_utils_tests()
 def test_device_name(self):
     with mock_py_config_context():
         run_all_device_name_tests()
 def test_get_current_timer_empty(self):
     with mock_py_config_context():
         self.context.power_timer_timers = []
         assert get_current_timer(self.context) == (None, None, None)
 def test_restore_times_without_existing_file(self):
     with mock_py_config_context():
         assert restore_timers() == ()