def test_learns_heat_for_time(self):
        t = Temperature(c, debug=True, has_heater=True)
        last_temp = 60.
        t.add(last_temp)
        t.update()

        increasing = False

        i = e = 0
        works = False
        while i < 10000:
            i += 1
            last_temp = temp_gen(last_temp, increasing)
            t.add(last_temp)
            t.update()

            if t.heating_on:
                e += 1
                maximum = t.temperature_average_f() + temp_by_s(t.heat_for_s)
                last_temp = maximum
                if abs(maximum - c.max_temp_f) < .05:
                    works = True
                    break
                [t.add(maximum + j*.01) for j in range(t.queue.cap)]
                t.heating_on = False
                t.waiting_for_temp_decrease = True

        self.assertTrue(works, msg="Failed to learn the correct temperature.")
        print('NOTE: heating steady after {} learned updates'.format(e))
    def test_is_decreasing_should_be_heating(self):
        t = Temperature(c, debug=True, has_heater=True, has_cooler=True)
        last_temp = 60.
        t.add(last_temp)
        t.update()
        increasing = False

        while True:
            last_temp = temp_gen(last_temp, increasing)
            t.add(last_temp)
            t.update()
            if not increasing and t.temperature_average_f() > c.min_temp_f:
                self.assertFalse(t.heating_on)
                increasing = False
            elif not increasing and t.temperature_average_f() <= c.min_temp_f:
                self.assertTrue(t.heating_on)
                increasing = True
            elif increasing and (now() - t.heater_enabled_at) >= timedelta(seconds=t.heat_for_s):
                self.assertFalse(t.heating_on)
                break
    def test_is_decreasing_should_not_be_cooling(self):
        t = Temperature(c, debug=True, has_heater=True, has_cooler=True)
        last_temp = 60.
        t.add(last_temp)
        t.update()

        while True:
            last_temp = temp_gen(last_temp, False)
            t.add(last_temp)
            t.update()
            if t.temperature_average_f() <= c.min_temp_f:
                self.assertTrue(t.heating_on)
                self.assertFalse(t.cooling_on)
                break
            else:
                self.assertFalse(t.cooling_on)
    def test_temp_avg(self):
        t = Temperature(c, debug=True)
        for i in range(1, 11, 1):
            t.add(i)

        self.assertEqual(5.5, t.temperature_average_f())
if __name__ == '__main__':
    try:
        log.info(80*"=")
        log.info("starting dht22_controller")
        log.info(80*"=")
        log.info("")
        
        # loop forever
        while True:
            h, t = get_data_wait()

            log.debug(
                'h=%.02f (avg=%.02f) dehumid=%s | t=%.02f (avg=%.02f) cool=%s',
                h, humidity.average(),
                'on' if humidity.dehumidifier_on else 'off',
                t, temperature.temperature_average_f(),
                'on' if temperature.cooling_on else 'off',
                )

            if conf.cool_pin is not None:
                g.output(conf.cool_pin, ON if temperature.cooling_on else OFF)

            if conf.dehumidity_pin is not None:
                g.output(conf.dehumidity_pin, ON if humidity.dehumidifier_on else OFF)

            time.sleep(1)
    except Exception as e:
        log.exception("an exception occurred in the main loop")
        raise