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 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 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 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 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 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 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