Пример #1
0
    def load(self, char_type: str):
        """
        This function loads a decoder for the specified characteristics:
         - get the name of the characteristic via the given uuid (via `CharacteristicsTypes.get_short()`)
         - load a module from `homekit.model.characteristics` plus the name of the characteristic
         - the module must contain a function `decoder`

        :param char_type: the uuid of the characteristic
        :return: a function that decodes the value of the characteristic into a `tlv8.EntryList`
        """
        characteristic_name = CharacteristicsTypes.get_short(char_type)
        if characteristic_name.startswith('Unknown'):
            mod_name = 'uuid_{}'.format(char_type.replace('-', '_'))
            logging.info('modname %s', mod_name)
        else:
            mod_name = characteristic_name.replace('-', '_')
        if char_type not in self.decoders:

            # try to dynamically load from the standard characteristics by name
            try:
                logging.info('loading module "%s" for type "%s"', mod_name, char_type)
                module = importlib.import_module('homekit.model.characteristics.' + mod_name)
                decoder = getattr(module, 'decoder')
                self.decoders[char_type] = decoder
                return decoder
            except Exception as e:
                logging.info('Error loading decoder: "%s" for type "%s"', e, char_type)

            # try to load from all plugins, it may be a non-standard characteristic with vendor specific data
            try:
                for _, plugin_name, _ in pkgutil.iter_modules():
                    if not plugin_name.startswith('homekit_'):
                        continue
                    logging.info('loading module "%s" for type "%s" from plugin "%s"', mod_name, char_type, plugin_name)
                    module = importlib.import_module('.model.characteristics.' + mod_name, plugin_name)
                    decoder = getattr(module, 'decoder')
                    self.decoders[char_type] = decoder
                    return decoder
            except Exception as e:
                logging.info('Error loading decoder: "%s" for type "%s"', e, char_type)

            return None
        else:
            logging.info('got decoder for %s from cache', char_type)
            return self.decoders[char_type]
 def test_get_short_unknown(self):
     self.assertEqual('Unknown Characteristic 1234',
                      CharacteristicsTypes.get_short('1234'))
 def test_get_short_short_uuid(self):
     self.assertEqual('position.current',
                      CharacteristicsTypes.get_short('6D'))
 def test_get_short_full_uuid(self):
     self.assertEqual(
         'position.current',
         CharacteristicsTypes.get_short(
             '0000006D-0000-1000-8000-0026BB765291'))