def dto_to_orm(room_dto, fields): # type: (RoomDTO, List[str]) -> Room room = Room.get_or_none(number=room_dto.id) if room is None: room = Room(number=room_dto.id) if 'name' in fields: room.name = room_dto.name return room
def dto_to_orm(room_dto): # type: (RoomDTO) -> Room room = Room.get_or_none(number=room_dto.id) if room is None: room = Room(number=room_dto.id) if 'name' in room_dto.loaded_fields: room.name = room_dto.name return room
def save_rooms(self, rooms): # type: (List[RoomDTO]) -> None _ = self with Database.get_db().atomic(): for room_dto in rooms: if room_dto.in_use: room = RoomMapper.dto_to_orm(room_dto) if 'floor' in room_dto.loaded_fields: floor = None if room_dto.floor is not None: floor = FloorMapper.dto_to_orm(room_dto.floor) floor.save() room.floor = floor room.save() else: Room.delete().where(Room.number == room_dto.id).execute()
def test_output_actions(self): floor = Floor.create(number=5) room = Room.create(number=10, floor=floor) Output.create(number=2, room=room) Output.create(number=3) with mock.patch.object(self.master_controller, 'set_all_lights') as call: self.controller.set_all_lights(action='OFF') call.assert_called_once_with(action='OFF') with mock.patch.object(self.master_controller, 'set_all_lights') as call: self.controller.set_all_lights(action='ON') call.assert_called_once_with(action='ON') with mock.patch.object(self.master_controller, 'set_all_lights') as call: self.controller.set_all_lights(action='OFF', floor_id=1) call.assert_called_once_with(action='OFF', floor_id=1, output_ids=[]) with mock.patch.object(self.master_controller, 'set_all_lights') as call: self.controller.set_all_lights(action='OFF', floor_id=5) call.assert_called_once_with(action='OFF', floor_id=5, output_ids=[2]) with mock.patch.object(self.master_controller, 'set_all_lights') as call: self.controller.set_all_lights(action='ON', floor_id=5) call.assert_called_once_with(action='ON', floor_id=5, output_ids=[2])
def load_rooms(self): # type: () -> List[RoomDTO] _ = self room_dtos = [] for room in list(Room.select(Room, Floor) .join_from(Room, Floor, join_type=JOIN.LEFT_OUTER)): # TODO: Load dicts room_dto = RoomMapper.orm_to_dto(room) if room.floor is not None: room_dto.floor = FloorMapper.orm_to_dto(room.floor) room_dtos.append(room_dto) return room_dtos
def load_room(self, room_id): # type: (int) -> RoomDTO _ = self room = Room.select(Room, Floor) \ .join_from(Room, Floor, join_type=JOIN.LEFT_OUTER) \ .where(Room.number == room_id) \ .get() # type: Room # TODO: Load dict room_dto = RoomMapper.orm_to_dto(room) if room.floor is not None: room_dto.floor = FloorMapper.orm_to_dto(room.floor) return room_dto
def test_load_outputs(self): select_mock = mock.Mock() select_mock.join_from.return_value = [ Output(id=1, number=42, room=Room(id=2, number=3)) ] with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', return_value=OutputDTO(id=42)) as load: outputs = self.controller.load_outputs() assert OutputDTO(id=42, room=3) in outputs load.assert_called_with(output_id=42)
def _get_or_create_room(eext_controller, room_id, rooms, floors, skip_empty=False): # type: (EepromExtension, int, Dict[int, Room], Dict[int, Floor], bool) -> Optional[Room] if room_id not in rooms: room = Room.get_or_none(number=room_id) if room is None: room_data = RoomsMigrator._read_eext_fields( eext_controller, 'RoomConfiguration', room_id, ['floor', 'name']) name = room_data.get('name', '') try: floor_id = int(room_data.get('floor', 255)) except ValueError: floor_id = 255 if skip_empty and name == '' and floor_id == 255: return None room = Room(number=room_id, name=name) if floor_id != 255: room.floor = RoomsMigrator._get_or_create_floor( eext_controller, floor_id, floors) room.save() RoomsMigrator._delete_eext_fields(eext_controller, 'RoomConfiguration', room_id, ['floor', 'name']) rooms[room_id] = room return rooms[room_id]
def save_shutter_groups(self, shutter_groups): # type: (List[Tuple[ShutterGroupDTO, List[str]]]) -> None shutter_groups_to_save = [] for shutter_group_dto, fields in shutter_groups: shutter_group = ShutterGroup.get_or_none(number=shutter_group_dto.id) # type: ShutterGroup if shutter_group is None: continue if 'room' in fields: if shutter_group_dto.room is None: shutter_group.room = None elif 0 <= shutter_group_dto.room <= 100: shutter_group.room, _ = Room.get_or_create(number=shutter_group_dto.room) shutter_group.save() shutter_groups_to_save.append((shutter_group_dto, fields)) self._master_controller.save_shutter_groups(shutter_groups_to_save)
def test_load_output(self): where_mock = mock.Mock() where_mock.get.return_value = Output(id=1, number=42, room=Room(id=2, number=3)) join_from_mock = mock.Mock() join_from_mock.where.return_value = where_mock select_mock = mock.Mock() select_mock.join_from.return_value = join_from_mock with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', return_value=OutputDTO(id=42)) as load: output = self.controller.load_output(42) assert output == OutputDTO(id=42, room=3) load.assert_called_with(output_id=42)
def test_get_output_status(self): select_mock = mock.Mock() select_mock.join_from.return_value = [ Output(id=0, number=2), Output(id=1, number=40, room=Room(id=2, number=3)) ] with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', side_effect=lambda output_id: OutputDTO(id=output_id)), \ mock.patch.object(self.master_controller, 'load_output_status', return_value=[OutputStateDTO(id=2, status=False), OutputStateDTO(id=40, status=True)]): self.controller._sync_state() status = self.controller.get_output_status(40) assert status == OutputStateDTO(id=40, status=True)
def save_shutters(self, shutters): # type: (List[Tuple[ShutterDTO, List[str]]]) -> None shutters_to_save = [] for shutter_dto, fields in shutters: shutter = Shutter.get_or_none(number=shutter_dto.id) # type: Shutter if shutter is None: logger.info('Ignored saving non-existing Shutter {0}'.format(shutter_dto.id)) if 'room' in fields: if shutter_dto.room is None: shutter.room = None elif 0 <= shutter_dto.room <= 100: shutter.room, _ = Room.get_or_create(number=shutter_dto.room) shutter.save() shutters_to_save.append((shutter_dto, fields)) self._master_controller.save_shutters(shutters_to_save) self.update_config(self.load_shutters())
def save_outputs(self, outputs): # type: (List[OutputDTO]) -> None outputs_to_save = [] for output_dto in outputs: output = Output.get_or_none(number=output_dto.id) # type: Output if output is None: logger.info('Ignored saving non-existing Output {0}'.format( output_dto.id)) if 'room' in output_dto.loaded_fields: if output_dto.room is None: output.room = None elif 0 <= output_dto.room <= 100: output.room, _ = Room.get_or_create(number=output_dto.room) output.save() outputs_to_save.append(output_dto) self._master_controller.save_outputs(outputs_to_save)
def test_get_output_statuses(self): select_mock = mock.Mock() select_mock.join_from.return_value = [ Output(id=0, number=2), Output(id=1, number=40, module_type='D', room=Room(id=2, number=3)) ] with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', side_effect=lambda output_id: OutputDTO(id=output_id)), \ mock.patch.object(self.master_controller, 'load_output_status', return_value=[OutputStateDTO(id=2, status=False, dimmer=0), OutputStateDTO(id=40, status=True, dimmer=50)]): self.controller._sync_state() status = self.controller.get_output_statuses() assert len(status) == 2 assert OutputStateDTO(id=2, status=False) in status assert OutputStateDTO(id=40, status=True, dimmer=50) in status
def save_sensors( self, sensors): # type: (List[Tuple[SensorDTO, List[str]]]) -> None sensors_to_save = [] for sensor_dto, fields in sensors: sensor_ = Sensor.get_or_none(number=sensor_dto.id) # type: Sensor if sensor_ is None: logger.info('Ignored saving non-existing Sensor {0}'.format( sensor_dto.id)) if 'room' in fields: if sensor_dto.room is None: sensor_.room = None elif 0 <= sensor_dto.room <= 100: sensor_.room, _ = Room.get_or_create( number=sensor_dto.room) sensor_.save() sensors_to_save.append((sensor_dto, fields)) self._master_controller.save_sensors(sensors_to_save)
def save_inputs(self, inputs): # type: (List[InputDTO]) -> None inputs_to_save = [] for input_dto in inputs: input_ = Input.get_or_none(number=input_dto.id) # type: Input if input_ is None: logger.info('Ignored saving non-existing Input {0}'.format( input_dto.id)) if 'event_enabled' in input_dto.loaded_fields: input_.event_enabled = input_dto.event_enabled input_.save() if 'room' in input_dto.loaded_fields: if input_dto.room is None: input_.room = None elif 0 <= input_dto.room <= 100: # TODO: Validation should happen on API layer input_.room, _ = Room.get_or_create(number=input_dto.room) input_.save() inputs_to_save.append(input_dto) self._master_controller.save_inputs(inputs_to_save)
def save_pulse_counters(self, pulse_counters): # type: (List[PulseCounterDTO]) -> None pulse_counters_to_save = [] for pulse_counter_dto in pulse_counters: pulse_counter = PulseCounter.get_or_none(number=pulse_counter_dto.id) # type: PulseCounter if pulse_counter is None: raise DoesNotExist('A PulseCounter with id {0} could not be found'.format(pulse_counter_dto.id)) if pulse_counter.source == 'master': # Only master pulse counters will be passed to the MasterController batch save pulse_counters_to_save.append(pulse_counter_dto) if 'name' in pulse_counter_dto.loaded_fields: pulse_counter.name = pulse_counter_dto.name elif pulse_counter.source == 'gateway': pulse_counter = PulseCounterMapper.dto_to_orm(pulse_counter_dto) else: logger.warning('Trying to save a PulseCounter with unknown source {0}'.format(pulse_counter.source)) continue if 'room' in pulse_counter_dto.loaded_fields: if pulse_counter_dto.room is None: pulse_counter.room = None elif 0 <= pulse_counter_dto.room <= 100: pulse_counter.room, _ = Room.get_or_create(number=pulse_counter_dto.room) pulse_counter.save() self._master_controller.save_pulse_counters(pulse_counters_to_save)
def test_save(self): temperatures = {} def _get_temperature(sensor_id): return temperatures[sensor_id] controller = GatewayThermostatMappingTests._create_controller( get_sensor_temperature_status=_get_temperature) room = Room(number=5) room.save() thermostat_group = ThermostatGroup(number=0, name='global') thermostat_group.save() thermostat = Thermostat( number=10, start=0, # 0 is on a thursday name='thermostat', thermostat_group=thermostat_group) thermostat.save() heating_thermostats = controller.load_heating_thermostats() self.assertEqual(1, len(heating_thermostats)) dto = heating_thermostats[0] # type: ThermostatDTO default_schedule_dto = ThermostatScheduleDTO(temp_day_1=20.0, start_day_1='07:00', end_day_1='09:00', temp_day_2=21.0, start_day_2='17:00', end_day_2='22:00', temp_night=16.0) Sensor.create(number=15) dto.room = 5 dto.sensor = 15 dto.output0 = 5 dto.name = 'changed' dto.auto_thu = ThermostatScheduleDTO(temp_night=10, temp_day_1=15, temp_day_2=30, start_day_1='08:00', end_day_1='10:30', start_day_2='16:00', end_day_2='18:45') temperatures[15] = 5.0 controller.save_heating_thermostats([dto]) heating_thermostats = controller.load_heating_thermostats() self.assertEqual(1, len(heating_thermostats)) dto = heating_thermostats[0] # type: ThermostatDTO self.assertEqual( ThermostatDTO(id=10, name='changed', setp3=16.0, setp4=15.0, setp5=22.0, sensor=15, pid_p=120.0, pid_i=0.0, pid_d=0.0, room=5, output0=5, permanent_manual=True, auto_mon=default_schedule_dto, auto_tue=default_schedule_dto, auto_wed=default_schedule_dto, auto_thu=ThermostatScheduleDTO(temp_night=10.0, temp_day_1=15.0, temp_day_2=30.0, start_day_1='08:00', end_day_1='10:30', start_day_2='16:00', end_day_2='18:45'), auto_fri=default_schedule_dto, auto_sat=default_schedule_dto, auto_sun=default_schedule_dto), dto)
def test_output_sync_change(self): events = [] def on_change(gateway_event): events.append(gateway_event) self.pubsub.subscribe_gateway_events(PubSub.GatewayTopics.STATE, on_change) outputs = {2: OutputDTO(id=2), 40: OutputDTO(id=40, module_type='D')} select_mock = mock.Mock() select_mock.join_from.return_value = [ Output(id=0, number=2), Output(id=1, number=40, room=Room(id=2, number=3)) ] with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', side_effect=lambda output_id: outputs.get(output_id)), \ mock.patch.object(self.master_controller, 'load_output_status', return_value=[OutputStateDTO(id=2, status=True), OutputStateDTO(id=40, status=True)]): self.controller._sync_state() self.pubsub._publish_all_events() assert [ GatewayEvent( 'OUTPUT_CHANGE', { 'id': 2, 'status': { 'on': True, 'locked': False }, 'location': { 'room_id': 255 } }), GatewayEvent( 'OUTPUT_CHANGE', { 'id': 40, 'status': { 'on': True, 'value': 0, 'locked': False }, 'location': { 'room_id': 3 } }) ] == events select_mock = mock.Mock() select_mock.join_from.return_value = [ Output(id=0, number=2), Output(id=1, number=40, room=Room(id=2, number=3)) ] with mock.patch.object(Output, 'select', return_value=select_mock), \ mock.patch.object(self.master_controller, 'load_output', side_effect=lambda output_id: outputs.get(output_id)), \ mock.patch.object(self.master_controller, 'load_output_status', return_value=[OutputStateDTO(id=2, status=True, dimmer=0), OutputStateDTO(id=40, status=True, dimmer=50)]): events = [] self.controller._sync_state() self.pubsub._publish_all_events() assert [ GatewayEvent( 'OUTPUT_CHANGE', { 'id': 2, 'status': { 'on': True, 'locked': False }, 'location': { 'room_id': 255 } }), GatewayEvent( 'OUTPUT_CHANGE', { 'id': 40, 'status': { 'on': True, 'value': 50, 'locked': False }, 'location': { 'room_id': 3 } }) ] == events