def test_handle_new_reading_off(): temp_controller = TemperatureController(20, 1, Mode.OFF) assert temp_controller.handle_new_reading(25) == State.ALL_OFF assert temp_controller.handle_new_reading(22) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20.9) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.ALL_OFF temp_controller.shutdown()
def test_handle_new_reading_cooler(): temp_controller = TemperatureController(20, 1, Mode.COOLER) assert temp_controller.handle_new_reading(25) == State.COOLER_ON assert temp_controller.handle_new_reading(22) == State.COOLER_ON assert temp_controller.handle_new_reading(20.1) == State.COOLER_ON assert temp_controller.handle_new_reading(20) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20.9) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.COOLER_ON temp_controller.shutdown()
def test_handle_new_reading_heater(): temp_controller = TemperatureController(20, 1, Mode.HEATER) assert temp_controller.handle_new_reading(15) == State.HEATER_ON assert temp_controller.handle_new_reading(19) == State.HEATER_ON assert temp_controller.handle_new_reading(19.9) == State.HEATER_ON assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.ALL_OFF assert temp_controller.handle_new_reading(19.9) == State.ALL_OFF assert temp_controller.handle_new_reading(19.1) == State.ALL_OFF assert temp_controller.handle_new_reading(19.0) == State.HEATER_ON temp_controller.shutdown()
class Thermostat(): def __init__(self, update_server): self.temperature_controller = TemperatureController(set_temperature=20, mode=Mode.COOLER) self.sensor_controller = SensorController() self.mqtt_controller = DataController() self.user_interface = UserInterface() self.unit_controller = UnitController() self.update_server = update_server self.command_subscriber = CommandSubscriber("192.168.3.2", self.handle_command) self.current_display_data = None def startup(self): pass def run(self): self.command_subscriber.subscribe() while True: current_temperature = self.sensor_controller.get_temperature() current_state = self.temperature_controller.handle_new_reading( current_temperature) current_humidity = self.sensor_controller.get_humidity() converted_data = self.unit_controller.convert({ "temperature": current_temperature, "set_temperature": self.temperature_controller.get_set_temperature(), "humidity": current_humidity, "mode": self.temperature_controller.get_mode(), "state": current_state }) self.mqtt_controller.publish_data(converted_data) self.update_server(converted_data) self.command_subscriber.command_loop() self.current_display_data = converted_data time.sleep(1) def handle_command(self, command): command_type = command.get('command') if command_type == "SET_TEMPERATURE": self.temperature_controller.update_set_temperature( self.unit_controller.convert_incomming( command['data']['temperature'])) elif command_type == "SET_MODE": self.temperature_controller.set_mode(Mode(command['data']['mode'])) def update_set_temperature(self, set_temperature): converted_temp = UnitController.convert_f_to_c(set_temperature) self.temperature_controller.update_set_temperature(converted_temp) self.update_server({ "temperature": self.current_display_data.get('temperature'), "set_temperature": set_temperature, "humidity": self.current_display_data.get('humidity'), "mode": self.temperature_controller.get_mode(), "state": self.current_display_data.get('state') }) def shutdown(self): print('Shutting Down') self.temperature_controller.shutdown()