attr_name="temperature",
                                         value=12))
    # Deleting attributes
    # logger.info(cb_client.delete_entity_attribute(entity_id=room1_entity.id,
    #                                               attr_name="temperature"))
    # ### 4.1.2 Updating the model
    #
    # Most of the time it is more convenient to update our local model,
    # and let the library handle all the needed updates to synchronise the
    # live state to the model state.
    # Hereby it is tried to only make changes that were done locally,
    # keeping as much of the current live state as possible

    # when accessing an attribute a new object is created. We need to
    # manually transmit the made changes.
    temp_attr = room2_entity.get_attribute("temperature")
    temp_attr.value = 15
    room2_entity.update_attribute([temp_attr])

    room2_entity.delete_attributes(["pressure"])

    # all changes are transmitted with one methode call
    cb_client.patch_entity(room2_entity)

    # ## 4.2 Deleting
    #
    # To delete an entry in Fiware, we can call:
    cb_client.delete_entity(entity_id=room2_entity.id,
                            entity_type=room2_entity.type)
    # cb_client.delete_entity(entity_id=room1_entity.id,
    #                         entity_type=room1_entity.type)
Exemple #2
0
    def _context_entity_to_semantic_class(
            self, entity: ContextEntity,
            header: InstanceHeader) -> SemanticClass:
        """Converts a ContextEntity to a SemanticClass

        Args:
            entity (ContextEntity): entity to convert
            header (InstanceHeader): Header of the new instance

        Returns:
            SemanticClass or SemanticDeviceClass
        """

        class_name = entity.type

        class_: Type = self.get_class_by_name(class_name)

        if not self.is_class_name_an_device_class(class_name):

            loaded_class: SemanticClass = class_(id=entity.id,
                                                 header=header,
                                                 enforce_new=True)
        else:
            loaded_class: SemanticDeviceClass = class_(id=entity.id,
                                                       header=header,
                                                       enforce_new=True)

        loaded_class.old_state.state = entity

        # load values of class from the context_entity into the instance
        for field in loaded_class.get_fields():
            field.clear()  # remove default values, from hasValue relations
            field_name = field.name
            entity_attribute = entity.get_attribute(field_name)
            if entity_attribute is None:
                raise Exception(
                    f"The corresponding entity for ({entity.id},{entity.type}) "
                    f"in Fiware misses a field that "
                    f"is required by the class_model: {field_name}. The "
                    f"fiware state and the used vocabulary models are not "
                    f"compatible")

            entity_field_value = entity.get_attribute(field_name).value

            if isinstance(entity_field_value, List):
                values = entity_field_value
            else:
                values = [entity_field_value]

            for value in values:
                converted_value = self._convert_value_fitting_for_field(
                    field, value)
                if isinstance(field, RelationField):
                    # we need to bypass the main setter, as it expects an
                    # instance and we do not want to load the instance if it
                    # is not used
                    field._set.add(converted_value)
                else:
                    field.add(converted_value)

        # load references into instance
        references_attribute = entity.get_attribute("referencedBy")
        references = references_attribute.value

        for identifier_str, prop_list in references.items():
            for prop in prop_list:
                loaded_class.add_reference(
                    InstanceIdentifier.parse_raw(
                        identifier_str.replace("---", ".")), prop)

        # load metadata
        metadata_dict = entity.get_attribute("metadata").value
        loaded_class.metadata.name = metadata_dict['name']
        loaded_class.metadata.comment = metadata_dict['comment']

        # load device_settings into instance, if instance is a device
        if isinstance(loaded_class, SemanticDeviceClass):
            settings_attribute = entity.get_attribute("deviceSettings")
            device_settings = DeviceSettings.parse_obj(
                settings_attribute.value)

            for key, value in device_settings.dict().items():
                loaded_class.device_settings.__setattr__(key, value)

        return loaded_class