def _ParseTranslation( translation_body: syaml.YAML) -> Dict[str, ft_lib.FieldTranslation]: """Parses YAML defining the translation of an entity's points. see: https://github.com/google/digitalbuildings/blob/master/ontology/docs/building_config.md#defining-translations Args: translation_body: YAML body for the entity translation Returns: A dictionary from field names to FieldTranslation instances """ if isinstance(translation_body, str): raise ValueError(translation_body + ' is not a valid translation') translation = {} for std_field_name in translation_body: ft = translation_body[std_field_name] if isinstance(ft, str): if not ft: raise ValueError( 'Translation details were empty for standard field name: ' + std_field_name) elif ft == ft_lib.PresenceMode.MISSING.value: translation[std_field_name] = ft_lib.UndefinedField( std_field_name) continue # TODO(b/187757180): support UDMI-compliant shorthand raise ValueError(ft + ' is not yet an allowed scalar') raw_field_name = str(ft[parse.PRESENT_VALUE_KEY]) ft_object = None if parse.UNITS_KEY in ft: unit_field_name = ft[parse.UNITS_KEY][parse.UNIT_NAME_KEY] unit_mappings = ft[parse.UNITS_KEY][parse.UNIT_VALUES_KEY] ft_object = ft_lib.DimensionalValue(std_field_name, raw_field_name, unit_field_name, unit_mappings) if parse.STATES_KEY in ft: if ft_object: raise ValueError( 'states and units are not allowed in the same translation') ft_object = ft_lib.MultiStateValue(std_field_name, raw_field_name, ft[parse.STATES_KEY]) if not ft_object: ft_object = ft_lib.NonDimensionalValue(std_field_name, raw_field_name) translation[std_field_name] = ft_object return translation
def testInstance_MultiStateTranslation_UndefinedField(self): entity = entity_instance.EntityInstance( _UPDATE, 'VAV-123', etag='1234', translation={ 'UNDEFINED_STATE': field_translation.MultiStateValue(std_field_name='foo/bar', raw_field_name='foo/raw', states={'foo': 'bar'}) }) self.assertFalse(self.update_validator.Validate(entity))
def testInstance_MultiStateTranslation_FieldHasInvalidState(self): entity = entity_instance.EntityInstance( _UPDATE, 'VAV-123', etag='1234', translation={ 'exhaust_air_damper_command': field_translation.MultiStateValue( std_field_name='exhaust_air_damper_command', raw_field_name='exhaust_air_damper_command', states={'INVALID_STATE': '1'}) }) self.assertFalse(self.update_validator.Validate(entity))
def testInstance_MultiStateTranslation_MissingStates(self): try: entity_instance.EntityInstance( _UPDATE, 'VAV-123', etag='1234', translation={ 'foo_bar': field_translation.MultiStateValue(std_field_name='foo/bar', raw_field_name='foo/raw', states={}) }) except ValueError as e: self.assertEqual(type(e), ValueError) else: self.fail('{ValueError} was not raised')
def testInstance_MultiStateTranslation_FieldIsValid(self): entity = entity_instance.EntityInstance( _UPDATE, 'VAV-123', etag='1234', translation={ 'exhaust_air_damper_command': field_translation.MultiStateValue( std_field_name='exhaust_air_damper_command', raw_field_name='exhaust_air_damper_command', states={ 'OPEN': '1', 'CLOSED': '0' }) }) self.assertTrue(self.update_validator.Validate(entity))