mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[core] Improve error reporting for entity name conflicts with non-ASCII characters
This commit is contained in:
		| @@ -236,10 +236,20 @@ def entity_duplicate_validator(platform: str) -> Callable[[ConfigType], ConfigTy | |||||||
|             if existing_component != "unknown": |             if existing_component != "unknown": | ||||||
|                 conflict_msg += f" from component '{existing_component}'" |                 conflict_msg += f" from component '{existing_component}'" | ||||||
|  |  | ||||||
|  |             # Show both original names and their ASCII-only versions if they differ | ||||||
|  |             sanitized_msg = "" | ||||||
|  |             if entity_name != name_key or existing_name != name_key: | ||||||
|  |                 sanitized_msg = ( | ||||||
|  |                     f"\n  Original names: '{entity_name}' and '{existing_name}'" | ||||||
|  |                     f"\n  Both convert to ASCII ID: '{name_key}'" | ||||||
|  |                     f"\n  To fix: Add unique ASCII characters (e.g., '1', '2', or 'A', 'B') to distinguish them" | ||||||
|  |                 ) | ||||||
|  |  | ||||||
|             raise cv.Invalid( |             raise cv.Invalid( | ||||||
|                 f"Duplicate {platform} entity with name '{entity_name}' found{device_prefix}. " |                 f"Duplicate {platform} entity with name '{entity_name}' found{device_prefix}. " | ||||||
|                 f"{conflict_msg}. " |                 f"{conflict_msg}. " | ||||||
|                 f"Each entity on a device must have a unique name within its platform." |                 f"Each entity on a device must have a unique name within its platform." | ||||||
|  |                 f"{sanitized_msg}" | ||||||
|             ) |             ) | ||||||
|  |  | ||||||
|         # Store metadata about this entity |         # Store metadata about this entity | ||||||
|   | |||||||
| @@ -705,3 +705,28 @@ def test_empty_or_null_device_id_on_entity() -> None: | |||||||
|     config2 = {CONF_NAME: "Temperature", CONF_DEVICE_ID: None} |     config2 = {CONF_NAME: "Temperature", CONF_DEVICE_ID: None} | ||||||
|     validated2 = validator(config2) |     validated2 = validator(config2) | ||||||
|     assert validated2 == config2 |     assert validated2 == config2 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def test_entity_duplicate_validator_non_ascii_names() -> None: | ||||||
|  |     """Test that non-ASCII names show helpful error messages.""" | ||||||
|  |     # Create validator for binary_sensor platform | ||||||
|  |     validator = entity_duplicate_validator("binary_sensor") | ||||||
|  |  | ||||||
|  |     # First Russian sensor should pass | ||||||
|  |     config1 = {CONF_NAME: "Датчик открытия основного крана"} | ||||||
|  |     validated1 = validator(config1) | ||||||
|  |     assert validated1 == config1 | ||||||
|  |  | ||||||
|  |     # Second Russian sensor with different text but same ASCII conversion should fail | ||||||
|  |     config2 = {CONF_NAME: "Датчик закрытия основного крана"} | ||||||
|  |     with pytest.raises( | ||||||
|  |         Invalid, | ||||||
|  |         match=re.compile( | ||||||
|  |             r"Duplicate binary_sensor entity with name 'Датчик закрытия основного крана' found.*" | ||||||
|  |             r"Original names: 'Датчик закрытия основного крана' and 'Датчик открытия основного крана'.*" | ||||||
|  |             r"Both convert to ASCII ID: '_______________________________'.*" | ||||||
|  |             r"To fix: Add unique ASCII characters \(e\.g\., '1', '2', or 'A', 'B'\)", | ||||||
|  |             re.DOTALL, | ||||||
|  |         ), | ||||||
|  |     ): | ||||||
|  |         validator(config2) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user