def test_light_off_program__should_not_turn_light_to_min_when_before_day_timer_on_wrong_day(
            self, mock_api, mock_date, mock_time):
        mock_date.datetime.now.return_value = self.SUNDAY
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_not_called()
    def test_light_off_program__should_not_turn_off_light_after_already_turning_off(
            self, mock_api, mock_date, mock_time):
        self.LIGHT_ON.TRIGGERED = True
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        mock_date.datetime.now.return_value = self.MONDAY
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_not_called()
    def test_light_off_program__should_not_turn_off_light_just_before_timer(
            self, mock_api, mock_date, mock_time):
        before_time = self.MONDAY + datetime.timedelta(minutes=-4, seconds=-1)
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        mock_date.datetime.now.return_value = before_time
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_not_called()
    def test_light_off_program__should_turn_light_to_min_when_after_timer_on_matching_day(
            self, mock_api, mock_date, mock_time):
        mock_date.datetime.now.return_value = self.MONDAY + datetime.timedelta(
            minutes=-4, seconds=30)
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_called_with(self.API_KEY, self.GROUP_ID, 0)
    def test_light_off_program__should_turn_off_light_when_exactly_at_timer(
            self, mock_api, mock_date, mock_time):
        exact_time = self.MONDAY + datetime.timedelta(minutes=-4)
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        mock_date.datetime.now.return_value = exact_time
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_called_with(self.API_KEY, self.GROUP_ID, 0)
        assert self.LIGHT_ON.TRIGGERED is True
    def test_light_off_program__should_not_retrigger_api_call_after_one_minute(
            self, mock_api, mock_date, mock_time):
        self.LIGHT_ON.TRIGGERED = False
        mock_date.datetime.combine.side_effect = datetime.datetime.combine
        mock_date.timedelta.side_effect = datetime.timedelta
        mock_date.date.today.return_value = datetime.datetime.today()
        one_minute_later = self.MONDAY + datetime.timedelta(minutes=-3,
                                                            seconds=1)
        mock_date.datetime.now.return_value = one_minute_later
        light_off_program(self.LIGHT_ON, self.API_KEY, self.GROUP_ID)

        mock_api.assert_not_called()
 def add_light_alarm(self, task_id, light_group_id, alarm_time, alarm_days,
                     task_type):
     if not any(alarm.THREAD_ID == task_id for alarm in self.LIGHT_ALARMS):
         if task_type == Automation.LIGHT.SUNRISE:
             alarm = LightAlarmState(task_id, alarm_time, alarm_days)
             alarm.ACTIVE_THREAD = create_thread(
                 lambda: light_alarm_program(alarm, self.get_light_api_key(
                 ), light_group_id), Automation.TIME.SEVEN_SECONDS)
             alarm.ACTIVE_THREAD.start()
             self.LIGHT_ALARMS.append(alarm)
         elif task_type == Automation.LIGHT.TURN_ON:
             alarm = LightOnOffState(task_id, alarm_time, alarm_days)
             alarm.ACTIVE_THREAD = create_thread(
                 lambda: light_on_program(alarm, self.get_light_api_key(
                 ), light_group_id), Automation.TIME.TEN_SECONDS)
             alarm.ACTIVE_THREAD.start()
             self.LIGHT_ALARMS.append(alarm)
         elif task_type == Automation.LIGHT.TURN_OFF:
             alarm = LightOnOffState(task_id, alarm_time, alarm_days)
             alarm.ACTIVE_THREAD = create_thread(
                 lambda: light_off_program(alarm, self.get_light_api_key(
                 ), light_group_id), Automation.TIME.TEN_SECONDS)
             alarm.ACTIVE_THREAD.start()
             self.LIGHT_ALARMS.append(alarm)