def deserialize( api_data): # type: (Dict) -> Tuple[ThermostatGroupDTO, List[str]] loaded_fields = [] thermostat_group_dto = ThermostatGroupDTO(id=0) loaded_fields += SerializerToolbox.deserialize( dto=thermostat_group_dto, # Referenced api_data=api_data, mapping={ 'outside_sensor': ('outside_sensor_id', ThermostatGroupSerializer.BYTE_MAX), 'threshold_temp': ('threshold_temperature', ThermostatGroupSerializer.BYTE_MAX), 'pump_delay': ('pump_delay', ThermostatGroupSerializer.BYTE_MAX) }) for mode in ['heating', 'cooling']: for i in range(4): output_field = 'switch_to_{0}_output_{1}'.format(mode, i) value_field = 'switch_to_{0}_value_{1}'.format(mode, i) dto_field = 'switch_to_{0}_{1}'.format(mode, i) if output_field in api_data and value_field in api_data: loaded_fields.append(dto_field) output = Toolbox.nonify(api_data[output_field], ThermostatGroupSerializer.BYTE_MAX) value = api_data[value_field] if output is None: setattr(thermostat_group_dto, dto_field, None) else: setattr(thermostat_group_dto, dto_field, [output, value]) return thermostat_group_dto, loaded_fields
def orm_to_dto(orm_object): # type: (EepromModel) -> ThermostatGroupDTO data = orm_object.serialize() kwargs = {} for dto_field, orm_field in {'outside_sensor_id': 'outside_sensor', 'threshold_temperature': 'threshold_temp', 'pump_delay': 'pump_delay'}.items(): kwargs[dto_field] = Toolbox.nonify(data[orm_field], ThermostatGroupMapper.BYTE_MAX) for mode in ['heating', 'cooling']: for i in range(4): output_field = 'switch_to_{0}_output_{1}'.format(mode, i) value_field = 'switch_to_{0}_value_{1}'.format(mode, i) dto_field = 'switch_to_{0}_{1}'.format(mode, i) output = Toolbox.nonify(data[output_field], ThermostatGroupMapper.BYTE_MAX) value = Toolbox.nonify(data[value_field], ThermostatGroupMapper.BYTE_MAX) if output is not None: kwargs[dto_field] = [output, value] return ThermostatGroupDTO(id=0, **kwargs)
def load_thermostat_group(self): # type: () -> ThermostatGroupDTO thermostat_group = ThermostatGroup.get(number=0) pump_delay = None for thermostat in thermostat_group.thermostats: for valve in thermostat.valves: pump_delay = valve.delay break sensor_number = None if thermostat_group.sensor is None else thermostat_group.sensor.number thermostat_group_dto = ThermostatGroupDTO( id=0, outside_sensor_id=sensor_number, threshold_temperature=thermostat_group.threshold_temperature, pump_delay=pump_delay) for link in OutputToThermostatGroup.select(OutputToThermostatGroup, Output) \ .join_from(OutputToThermostatGroup, Output) \ .where(OutputToThermostatGroup.thermostat_group == thermostat_group): if link.index > 3 or link.output is None: continue field = 'switch_to_{0}_{1}'.format(link.mode, link.index) setattr(thermostat_group_dto, field, (link.output.number, link.value)) return thermostat_group_dto
def test_thermostat_group_crud(self): thermostat = Thermostat.create(number=1, name='thermostat 1', sensor=Sensor.create(number=10), pid_heating_p=200, pid_heating_i=100, pid_heating_d=50, pid_cooling_p=200, pid_cooling_i=100, pid_cooling_d=50, automatic=True, room=None, start=0, valve_config='equal', thermostat_group=self._thermostat_group) Output.create(number=1) Output.create(number=2) Output.create(number=3) valve_output = Output.create(number=4) valve = Valve.create(number=1, name='valve 1', output=valve_output) ValveToThermostat.create(thermostat=thermostat, valve=valve, mode=ThermostatGroup.Modes.HEATING, priority=0) thermostat_group = ThermostatGroup.get(number=0) # type: ThermostatGroup self.assertEqual(10.0, thermostat_group.threshold_temperature) self.assertEqual(0, OutputToThermostatGroup.select() .where(OutputToThermostatGroup.thermostat_group == thermostat_group) .count()) self._thermostat_controller.save_thermostat_group((ThermostatGroupDTO(id=0, outside_sensor_id=1, pump_delay=30, threshold_temperature=15, switch_to_heating_0=(1, 0), switch_to_heating_1=(2, 100), switch_to_cooling_0=(1, 100)), ['outside_sensor_id', 'pump_delay', 'threshold_temperature', 'switch_to_heating_0', 'switch_to_heating_1', 'switch_to_cooling_0'])) thermostat_group = ThermostatGroup.get(number=0) self.assertEqual(15.0, thermostat_group.threshold_temperature) links = [{'index': link.index, 'value': link.value, 'mode': link.mode, 'output': link.output_id} for link in (OutputToThermostatGroup.select() .where(OutputToThermostatGroup.thermostat_group == thermostat_group))] self.assertEqual(3, len(links)) self.assertIn({'index': 0, 'value': 0, 'mode': 'heating', 'output': 1}, links) self.assertIn({'index': 1, 'value': 100, 'mode': 'heating', 'output': 2}, links) self.assertIn({'index': 0, 'value': 100, 'mode': 'cooling', 'output': 1}, links) new_thermostat_group_dto = ThermostatGroupDTO(id=0, outside_sensor_id=1, pump_delay=60, threshold_temperature=10, switch_to_heating_0=(1, 50), switch_to_cooling_0=(2, 0)) self._thermostat_controller.save_thermostat_group((new_thermostat_group_dto, ['outside_sensor_id', 'pump_delay', 'threshold_temperature', 'switch_to_heating_0', 'switch_to_heating_1', 'switch_to_cooling_0'])) thermostat_group = ThermostatGroup.get(number=0) self.assertEqual(10.0, thermostat_group.threshold_temperature) links = [{'index': link.index, 'value': link.value, 'mode': link.mode, 'output': link.output_id} for link in (OutputToThermostatGroup.select() .where(OutputToThermostatGroup.thermostat_group == thermostat_group))] self.assertEqual(2, len(links)) self.assertIn({'index': 0, 'value': 50, 'mode': 'heating', 'output': 1}, links) self.assertIn({'index': 0, 'value': 0, 'mode': 'cooling', 'output': 2}, links) self.assertEqual(new_thermostat_group_dto, self._thermostat_controller.load_thermostat_group())