Improve test coverage for ESPHome manager (#120400)

This commit is contained in:
J. Nick Koston 2024-06-25 13:14:11 +02:00 committed by GitHub
parent 76e890865e
commit c15718519b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 6 deletions

View File

@ -263,6 +263,7 @@ async def _mock_generic_device_entry(
mock_list_entities_services: tuple[list[EntityInfo], list[UserService]],
states: list[EntityState],
entry: MockConfigEntry | None = None,
hass_storage: dict[str, Any] | None = None,
) -> MockESPHomeDevice:
if not entry:
entry = MockConfigEntry(
@ -286,6 +287,17 @@ async def _mock_generic_device_entry(
}
device_info = DeviceInfo(**(default_device_info | mock_device_info))
if hass_storage:
storage_key = f"{DOMAIN}.{entry.entry_id}"
hass_storage[storage_key] = {
"version": 1,
"minor_version": 1,
"key": storage_key,
"data": {
"device_info": device_info.to_dict(),
},
}
mock_device = MockESPHomeDevice(entry, mock_client, device_info)
def _subscribe_states(callback: Callable[[EntityState], None]) -> None:
@ -453,6 +465,7 @@ async def mock_bluetooth_entry_with_legacy_adv(
@pytest.fixture
async def mock_generic_device_entry(
hass: HomeAssistant,
hass_storage: dict[str, Any],
) -> Callable[
[APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockConfigEntry],
@ -464,10 +477,17 @@ async def mock_generic_device_entry(
entity_info: list[EntityInfo],
user_service: list[UserService],
states: list[EntityState],
mock_storage: bool = False,
) -> MockConfigEntry:
return (
await _mock_generic_device_entry(
hass, mock_client, {}, (entity_info, user_service), states
hass,
mock_client,
{},
(entity_info, user_service),
states,
None,
hass_storage if mock_storage else None,
)
).entry
@ -477,6 +497,7 @@ async def mock_generic_device_entry(
@pytest.fixture
async def mock_esphome_device(
hass: HomeAssistant,
hass_storage: dict[str, Any],
) -> Callable[
[APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice],
@ -485,19 +506,21 @@ async def mock_esphome_device(
async def _mock_device(
mock_client: APIClient,
entity_info: list[EntityInfo],
user_service: list[UserService],
states: list[EntityState],
entity_info: list[EntityInfo] | None = None,
user_service: list[UserService] | None = None,
states: list[EntityState] | None = None,
entry: MockConfigEntry | None = None,
device_info: dict[str, Any] | None = None,
mock_storage: bool = False,
) -> MockESPHomeDevice:
return await _mock_generic_device_entry(
hass,
mock_client,
device_info or {},
(entity_info, user_service),
states,
(entity_info or [], user_service or []),
states or [],
entry,
hass_storage if mock_storage else None,
)
return _mock_device

View File

@ -1156,3 +1156,28 @@ async def test_start_reauth(
assert len(flows) == 1
flow = flows[0]
assert flow["context"]["source"] == "reauth"
async def test_entry_missing_unique_id(
hass: HomeAssistant,
mock_client: APIClient,
mock_esphome_device: Callable[
[APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice],
],
) -> None:
"""Test the unique id is added from storage if available."""
entry = MockConfigEntry(
domain=DOMAIN,
unique_id=None,
data={
CONF_HOST: "test.local",
CONF_PORT: 6053,
CONF_PASSWORD: "",
},
options={CONF_ALLOW_SERVICE_CALLS: True},
)
entry.add_to_hass(hass)
await mock_esphome_device(mock_client=mock_client, mock_storage=True)
await hass.async_block_till_done()
assert entry.unique_id == "11:22:33:44:55:aa"