Пример #1
0
    def test_updates_pid_values_on_control(self, pid_mock_class):
        pid_mock = pid_mock_class.return_value

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0.5, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        self.config.get.side_effect = lambda key: {
            "p": 4,
            "i": 5,
            "d": 6,
            "target": 23,
            "heating_limit": 40,
            "limit_window": 5
        }.get(key)

        pid_mock.return_value = 5

        controller.control()

        tunings_mock.assert_called_with((4, 5, 6))
Пример #2
0
    def test_switch_heating_and_cooling_without_threshold(
            self, pid_mock_class, control, h_current, h_switch, c_current,
            c_switch):
        pid_mock = pid_mock_class.return_value

        self.heater.get.return_value = h_current
        self.cooler.get.return_value = c_current

        pid_mock.return_value = control

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        controller.control()

        if h_switch is None:
            self.heater.set.assert_not_called()
        else:
            self.heater.set.assert_called_with(h_switch)

        if c_switch is None:
            self.cooler.set.assert_not_called()
        else:
            self.cooler.set.assert_called_with(c_switch)
Пример #3
0
    def test_only_updates_pid_values_if_changed(self, pid_mock_class):
        pid_mock = pid_mock_class.return_value

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0.5, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        pid_mock.return_value = 5

        controller.control()

        tunings_mock.assert_called_once_with()  # the getter, not the setter
Пример #4
0
    def test_publishes_to_listeners(self, pid_mock_class):
        pid_mock = pid_mock_class.return_value

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0.5, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [self.listener])

        pid_mock.return_value = .4

        controller.control()

        self.listener.handle_controller.assert_called_with(4, 3, 2, .4)
Пример #5
0
    def test_dont_switch_heating_on_if_under_heating_threshold(
            self, pid_mock_class):
        pid_mock = pid_mock_class.return_value

        self.heater.get.return_value = False

        pid_mock.return_value = 0.4

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0.5, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        controller.control()

        self.heater.set.assert_not_called()
Пример #6
0
    def test_feeds_current_temperature_to_pid(self, pid_mock_class):
        pid_mock = pid_mock_class.return_value

        current_temp = Mock()
        self.current_temp.get_average.return_value = current_temp

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0.5, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        pid_mock.return_value = 5

        controller.control()

        pid_mock.assert_called_with(current_temp)
Пример #7
0
    def test_switch_off_heating_if_over_heating_limit(self, pid_mock_class,
                                                      fridge_temperature,
                                                      cb_current, cb_switch,
                                                      h_current, h_switch):
        pid_mock = pid_mock_class.return_value

        self.fridge_temp.get.return_value = fridge_temperature

        self.heater.get.return_value = h_current
        self.cooler.get.return_value = False
        self.limiter.get.return_value = cb_current

        pid_mock.return_value = 3  # would trigger the heater

        tunings_mock = PropertyMock()
        type(pid_mock).tunings = tunings_mock
        tunings_mock.return_value = (1, 2, 3)

        components_mock = PropertyMock()
        type(pid_mock).components = components_mock
        components_mock.return_value = (4, 3, 2)

        controller = Controller(self.config, 5, 0, self.heater, self.cooler,
                                self.limiter, self.current_temp,
                                self.fridge_temp, [])

        controller.control()

        if cb_switch is None:
            self.limiter.set.assert_not_called()
        else:
            self.limiter.set.assert_called_with(cb_switch)

        if h_switch is None:
            self.heater.set.assert_not_called()
        else:
            self.heater.set.assert_called_with(h_switch)