diff --git a/filip/models/ngsi_v2/iot.py b/filip/models/ngsi_v2/iot.py index 105b33b4..d6950954 100644 --- a/filip/models/ngsi_v2/iot.py +++ b/filip/models/ngsi_v2/iot.py @@ -14,9 +14,10 @@ BaseAttribute, \ BaseValueAttribute, \ BaseNameAttribute -from filip.utils.validators import (validate_fiware_datatype_string_protect, validate_fiware_datatype_standard, - validate_jexl_expression, validate_device_expression_language, - validate_service_group_expression_language) +from filip.utils.validators import (validate_fiware_datatype_string_protect, + validate_fiware_datatype_standard, + validate_jexl_expression, + validate_expression_language) logger = logging.getLogger() @@ -250,13 +251,13 @@ def validate_cbHost(cls, value): "IoT Agents to store information along with the devices " "in the Device Registry." ) - expressionLanguage: ExpressionLanguage = Field( + expressionLanguage: Optional[ExpressionLanguage] = Field( default=ExpressionLanguage.JEXL, description="optional boolean value, to set expression language used " "to compute expressions, possible values are: " - "legacy or jexl, but legacy is deprecated." + "legacy or jexl, but legacy is deprecated. If it is set None, jexl is used." ) - valid_expressionLanguage = field_validator("expressionLanguage")(validate_service_group_expression_language) + valid_expressionLanguage = field_validator("expressionLanguage")(validate_expression_language) explicitAttrs: Optional[bool] = Field( default=False, description="optional boolean value, to support selective ignore " @@ -321,13 +322,13 @@ class DeviceSettings(BaseModel): description="Name of the device transport protocol, for the IoT Agents " "with multiple transport protocols." ) - expressionLanguage: ExpressionLanguage = Field( + expressionLanguage: Optional[ExpressionLanguage] = Field( default=ExpressionLanguage.JEXL, description="optional boolean value, to set expression language used " "to compute expressions, possible values are: " - "legacy or jexl, but legacy is deprecated." + "legacy or jexl, but legacy is deprecated. If it is set None, jexl is used." ) - valid_expressionLanguage = field_validator("expressionLanguage")(validate_device_expression_language) + valid_expressionLanguage = field_validator("expressionLanguage")(validate_expression_language) explicitAttrs: Optional[bool] = Field( default=False, description="optional boolean value, to support selective ignore " diff --git a/filip/utils/validators.py b/filip/utils/validators.py index 7f8ee218..551574b9 100644 --- a/filip/utils/validators.py +++ b/filip/utils/validators.py @@ -220,16 +220,10 @@ def validate_jexl_expression(expression, attribute_name, device_id): return expression -def validate_device_expression_language(cls, expressionLanguage): +def validate_expression_language(cls, expressionLanguage): if expressionLanguage == "legacy": - warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is deprecated. Use 'JEXL' instead.") - - return expressionLanguage - - -def validate_service_group_expression_language(cls, expressionLanguage): - if expressionLanguage == "legacy": - warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is deprecated and does not work " - f"anymore, because each device uses 'JEXL' as default.") - + warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is " + f"deprecated. Use 'JEXL' instead.") + elif expressionLanguage is None: + expressionLanguage = "jexl" return expressionLanguage diff --git a/tests/models/test_ngsi_v2_iot.py b/tests/models/test_ngsi_v2_iot.py index fd1a3c13..fe99111b 100644 --- a/tests/models/test_ngsi_v2_iot.py +++ b/tests/models/test_ngsi_v2_iot.py @@ -215,6 +215,22 @@ def test_expression_language(self): assert len(w) == 2 + # Test for expression language set to None + service_group_null_expression = ServiceGroup( + entity_type='Thing', + apikey=api_key, + resource='/iot/json', + expressionLanguage=None) + self.assertEqual(service_group_null_expression.expressionLanguage, ExpressionLanguage.JEXL) + + device4 = Device(device_id="null_expression_device", + entity_name="null_expression_entity", + entity_type="test_entity_type", + transport=TransportProtocol.MQTT, + protocol=PayloadProtocol.IOTA_JSON, + expressionLanguage=None) + self.assertEqual(device4.expressionLanguage, ExpressionLanguage.JEXL) + def test_add_device_attributes(self): """ Test the device model regarding the behavior with devices attributes. diff --git a/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language.py b/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language.py index 9e2b6966..ad44cb0a 100644 --- a/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language.py +++ b/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language.py @@ -33,6 +33,7 @@ ExpressionLanguage) from filip.utils.cleanup import clear_all from paho.mqtt import client as mqtt_client +from paho.mqtt.client import CallbackAPIVersion # Host address of Context Broker CB_URL = "http://localhost:1026" @@ -103,7 +104,7 @@ ) iota_client.post_device(device=device2) - client = mqtt_client.Client() + client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2) client.username_pw_set(username="", password="") client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT) client.loop_start() @@ -147,7 +148,7 @@ ) iota_client.post_device(device=device3) - client = mqtt_client.Client() + client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2) client.username_pw_set(username="", password="") client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT) client.loop_start() diff --git a/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language_solution.py b/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language_solution.py index 2c20098c..b826d288 100644 --- a/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language_solution.py +++ b/tutorials/ngsi_v2/e8_multientity_and_expression_language/e8_multientity_and_expression_language_solution.py @@ -33,6 +33,7 @@ ExpressionLanguage) from filip.utils.cleanup import clear_all from paho.mqtt import client as mqtt_client +from paho.mqtt.client import CallbackAPIVersion # Host address of Context Broker CB_URL = "http://localhost:1026" @@ -114,7 +115,7 @@ ) iota_client.post_device(device=device2) - client = mqtt_client.Client() + client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2) client.username_pw_set(username="", password="") client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT) client.loop_start() @@ -172,7 +173,7 @@ ) iota_client.post_device(device=device3) - client = mqtt_client.Client() + client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2) client.username_pw_set(username="", password="") client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT) client.loop_start()