コード例 #1
0
ファイル: ventilation.py プロジェクト: rolaya/gateway
 def dto_to_orm(ventilation_dto, fields):
     # type: (VentilationDTO, List[str]) -> Ventilation
     lookup_kwargs = {}  # type: Dict[str,Any]
     if ventilation_dto.id:
         lookup_kwargs.update({'id': ventilation_dto.id})
     if ventilation_dto.source.is_plugin:
         plugin = Plugin.get(name=ventilation_dto.source.name)
         lookup_kwargs.update({
             'plugin': plugin,
             'source': ventilation_dto.source.type,
             'external_id': ventilation_dto.external_id
         })
     ventilation = Ventilation.get_or_none(**lookup_kwargs)
     if ventilation is None:
         ventilation = Ventilation(**lookup_kwargs)
     if 'name' in fields:
         ventilation.name = ventilation_dto.name
     if 'amount_of_levels' in fields:
         ventilation.amount_of_levels = ventilation_dto.amount_of_levels
     if 'device' in fields:
         ventilation.device_vendor = ventilation_dto.device_vendor
         ventilation.device_type = ventilation_dto.device_type
         if ventilation_dto.device_serial:
             ventilation.device_serial = ventilation_dto.device_serial
     return ventilation
コード例 #2
0
 def test_set_level(self):
     plugin = Plugin(id=2, name='dummy', version='0.0.1')
     with mock.patch.object(Select, 'count', return_value=1), \
          mock.patch.object(Ventilation, 'get',
                            side_effect=[Ventilation(id=42, amount_of_levels=4, source='plugin', plugin=plugin),
                                         Ventilation(id=43, amount_of_levels=4, source='plugin', plugin=plugin)]), \
          mock.patch.object(Ventilation, 'select',
                            return_value=[Ventilation(id=42, amount_of_levels=4, source='plugin', plugin=plugin),
                                          Ventilation(id=43, amount_of_levels=4, source='plugin', plugin=plugin)]):
         self.controller.set_level(42, 0)
         self.controller.set_level(43, 2, timer=60.0)
         status = self.controller.get_status()
         assert {'manual'} == set(x.mode for x in status)
         assert {42, 43} == set(x.id for x in status)
         assert {0, 2} == set(x.level for x in status)
         assert {None, 60.0} == set(x.timer for x in status)
コード例 #3
0
 def test_update_existing_ventilation(self):
     plugin = Plugin(id=2, name='dummy', version='0.0.1')
     with mock.patch.object(Plugin, 'get', return_value=plugin), \
          mock.patch.object(Ventilation, 'get_or_none',
                            return_value=Ventilation(id=42,
                                                     source='plugin',
                                                     source_id=2,
                                                     external_id='device-000001',
                                                     name='foo',
                                                     amount_of_levels=4,
                                                     device_type='model-0',
                                                     device_vendor='example',
                                                     device_serial='device-000001',
                                                     plugin=plugin)) as get_or_none, \
          mock.patch.object(Ventilation, 'save', return_value=1) as save:
         ventilation_dto = VentilationDTO(id=42,
                                          external_id='device-000001',
                                          source=VentilationSourceDTO(
                                              id=2,
                                              name='dummy',
                                              type='plugin'),
                                          name='foo',
                                          amount_of_levels=4,
                                          device_vendor='example',
                                          device_type='model-0',
                                          device_serial='device-000001')
         self.controller.save_ventilation(ventilation_dto)
         get_or_none.assert_called_with(id=42,
                                        source='plugin',
                                        plugin=plugin,
                                        external_id='device-000001')
         save.assert_called()
コード例 #4
0
 def test_load_ventilation(self):
     with mock.patch.object(Ventilation,
                            'get',
                            return_value=Ventilation(
                                id=42,
                                source='plugin',
                                external_id='device-000001',
                                name='foo',
                                amount_of_levels=4,
                                device_vendor='example',
                                device_type='model-0',
                                device_serial='device-000001',
                                plugin=Plugin(id=2,
                                              name='dummy',
                                              version='0.0.1'))):
         ventilation_dto = self.controller.load_ventilation(42)
         assert ventilation_dto == VentilationDTO(
             id=42,
             external_id='device-000001',
             source=VentilationSourceDTO(id=2, name='dummy', type='plugin'),
             name='foo',
             amount_of_levels=4,
             device_vendor='example',
             device_type='model-0',
             device_serial='device-000001')
コード例 #5
0
 def test_set_invalid_level(self):
     plugin = Plugin(id=2, name='dummy', version='0.0.1')
     with mock.patch.object(Select, 'count', return_value=1), \
          mock.patch.object(Ventilation, 'get',
                            return_value=Ventilation(id=42, amount_of_levels=4, souurce='plugin', plugin=plugin)):
         self.assertRaises(ValueError, self.controller.set_level, 42, 5)
         self.assertRaises(ValueError, self.controller.set_level, 42, -1)
コード例 #6
0
 def get_status(self):
     # type: () -> List[VentilationStatusDTO]
     status = []
     for ventilation in Ventilation.select():
         state_dto = self._status.get(ventilation.id)
         if state_dto:
             status.append(state_dto)
     return status
コード例 #7
0
    def test_ventilation_config_events(self):
        plugin = Plugin(id=2, name='dummy', version='0.0.1')
        with mock.patch.object(Plugin, 'get', return_value=plugin), \
                mock.patch.object(Ventilation, 'get_or_none',
                                  return_value=Ventilation(id=42,
                                                           source='plugin',
                                                           source_id=2,
                                                           external_id='device-000001',
                                                           name='foo',
                                                           amount_of_levels=4,
                                                           device_type='model-0',
                                                           device_vendor='example',
                                                           device_serial='device-000001',
                                                           plugin=plugin)), \
                mock.patch.object(Ventilation, 'save', side_effect=(0, 1)):

            events = []

            def callback(event):
                events.append(event)

            self.pubsub.subscribe_gateway_events(PubSub.GatewayTopics.CONFIG,
                                                 callback)

            ventilation_dto = VentilationDTO(id=42,
                                             external_id='device-000001',
                                             source=VentilationSourceDTO(
                                                 id=2,
                                                 name='dummy',
                                                 type='plugin'),
                                             name='foo',
                                             amount_of_levels=4,
                                             device_vendor='example',
                                             device_type='model-0',
                                             device_serial='device-000001')
            self.controller.save_ventilation(ventilation_dto)
            self.pubsub._publish_all_events()
            assert len(events) == 0, events  # No change

            ventilation_dto.name = 'bar'
            self.controller.save_ventilation(ventilation_dto)
            self.pubsub._publish_all_events()
            assert GatewayEvent(GatewayEvent.Types.CONFIG_CHANGE,
                                {'type': 'ventilation'}) in events
            assert len(events) == 1, events
コード例 #8
0
 def get_ventilation(id):
     return Ventilation(id=id,
                        amount_of_levels=4,
                        source='plugin',
                        plugin=plugin)
コード例 #9
0
 def load_ventilation(self, ventilation_id):
     # type: (int) -> VentilationDTO
     ventilation = Ventilation.get(id=ventilation_id)
     return VentilationMapper.orm_to_dto(ventilation)
コード例 #10
0
 def load_ventilations(self):
     # type: () -> List[VentilationDTO]
     return [
         VentilationMapper.orm_to_dto(ventilation)
         for ventilation in Ventilation.select()
     ]