mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[template] Store initial option as index in template select (#11523)
This commit is contained in:
		| @@ -41,6 +41,17 @@ select: | ||||
|       - ""  # Empty string at the end | ||||
|     initial_option: "Choice X" | ||||
|  | ||||
|   - platform: template | ||||
|     name: "Select Initial Option Test" | ||||
|     id: select_initial_option_test | ||||
|     optimistic: true | ||||
|     options: | ||||
|       - "First" | ||||
|       - "Second" | ||||
|       - "Third" | ||||
|       - "Fourth" | ||||
|     initial_option: "Third"  # Test non-default initial option | ||||
|  | ||||
| # Add a sensor to ensure we have other entities in the list | ||||
| sensor: | ||||
|   - platform: template | ||||
|   | ||||
| @@ -36,8 +36,8 @@ async def test_host_mode_empty_string_options( | ||||
|  | ||||
|         # Find our select entities | ||||
|         select_entities = [e for e in entity_info if isinstance(e, SelectInfo)] | ||||
|         assert len(select_entities) == 3, ( | ||||
|             f"Expected 3 select entities, got {len(select_entities)}" | ||||
|         assert len(select_entities) == 4, ( | ||||
|             f"Expected 4 select entities, got {len(select_entities)}" | ||||
|         ) | ||||
|  | ||||
|         # Verify each select entity by name and check their options | ||||
| @@ -71,6 +71,15 @@ async def test_host_mode_empty_string_options( | ||||
|         assert empty_last.options[2] == "Choice Z" | ||||
|         assert empty_last.options[3] == ""  # Empty string at end | ||||
|  | ||||
|         # Check "Select Initial Option Test" - verify non-default initial option | ||||
|         assert "Select Initial Option Test" in selects_by_name | ||||
|         initial_option_test = selects_by_name["Select Initial Option Test"] | ||||
|         assert len(initial_option_test.options) == 4 | ||||
|         assert initial_option_test.options[0] == "First" | ||||
|         assert initial_option_test.options[1] == "Second" | ||||
|         assert initial_option_test.options[2] == "Third" | ||||
|         assert initial_option_test.options[3] == "Fourth" | ||||
|  | ||||
|         # If we got here without protobuf decoding errors, the fix is working | ||||
|         # The bug would have caused "Invalid protobuf message" errors with trailing bytes | ||||
|  | ||||
| @@ -78,7 +87,12 @@ async def test_host_mode_empty_string_options( | ||||
|         # This ensures empty strings work properly in state messages too | ||||
|         states: dict[int, EntityState] = {} | ||||
|         states_received_future: asyncio.Future[None] = loop.create_future() | ||||
|         expected_select_keys = {empty_first.key, empty_middle.key, empty_last.key} | ||||
|         expected_select_keys = { | ||||
|             empty_first.key, | ||||
|             empty_middle.key, | ||||
|             empty_last.key, | ||||
|             initial_option_test.key, | ||||
|         } | ||||
|         received_select_keys = set() | ||||
|  | ||||
|         def on_state(state: EntityState) -> None: | ||||
| @@ -109,6 +123,14 @@ async def test_host_mode_empty_string_options( | ||||
|         assert empty_first.key in states | ||||
|         assert empty_middle.key in states | ||||
|         assert empty_last.key in states | ||||
|         assert initial_option_test.key in states | ||||
|  | ||||
|         # Verify the initial option is set correctly to "Third" (not the default "First") | ||||
|         initial_state = states[initial_option_test.key] | ||||
|         assert initial_state.state == "Third", ( | ||||
|             f"Expected initial state 'Third' but got '{initial_state.state}' - " | ||||
|             f"initial_option not correctly applied" | ||||
|         ) | ||||
|  | ||||
|         # The main test is that we got here without protobuf errors | ||||
|         # The select entities with empty string options were properly encoded | ||||
|   | ||||
		Reference in New Issue
	
	Block a user