Files
core/tests/components/fing/test_device_tracker.py
Lorenzo Gasparini a571271f6a Add Fing integration (#126058)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Norbert Rittel <norbert@rittel.de>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2025-10-24 15:45:00 +02:00

128 lines
4.0 KiB
Python

"""Test Fing Agent device tracker entity."""
from datetime import timedelta
from fing_agent_api.models import DeviceResponse
from freezegun.api import FrozenDateTimeFactory
from homeassistant.components.fing.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.common import (
AsyncMock,
async_fire_time_changed,
async_load_json_object_fixture,
snapshot_platform,
)
from tests.conftest import MockConfigEntry, SnapshotAssertion
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
mocked_fing_agent: AsyncMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities created by Fing with snapshot."""
entry = await init_integration(hass, mock_config_entry, mocked_fing_agent)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
async def test_new_device_found(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mocked_fing_agent: AsyncMock,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test Fing device tracker setup."""
delta_time = timedelta(seconds=35) # 30 seconds + 5 delta seconds
await hass.config.async_set_time_zone("UTC")
freezer.move_to("2021-01-09 12:00:00+00:00")
entry = await init_integration(hass, mock_config_entry, mocked_fing_agent)
# First check -> there are 3 devices in total
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 3
mocked_fing_agent.get_devices.return_value = DeviceResponse(
await async_load_json_object_fixture(
hass, "device_resp_device_added.json", DOMAIN
)
)
freezer.tick(delta_time)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Second check -> added one device
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 4
mocked_fing_agent.get_devices.return_value = DeviceResponse(
await async_load_json_object_fixture(
hass, "device_resp_device_deleted.json", DOMAIN
)
)
freezer.tick(delta_time)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Third check -> removed two devices (old devices)
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 2
async def test_device_modified(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mocked_fing_agent: AsyncMock,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test Fing device modified."""
delta_time = timedelta(seconds=35) # 30 seconds + 5 delta seconds
# ----------------------------------------------------
await hass.config.async_set_time_zone("UTC")
freezer.move_to("2021-01-09 12:00:00+00:00")
entry = await init_integration(hass, mock_config_entry, mocked_fing_agent)
old_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
# ----------------------------------------------------
mocked_fing_agent.get_devices.return_value = DeviceResponse(
await async_load_json_object_fixture(
hass, "device_resp_device_modified.json", DOMAIN
)
)
freezer.tick(delta_time)
async_fire_time_changed(hass)
await hass.async_block_till_done()
new_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
# ----------------------------------------------------
assert len(old_entries) == len(new_entries)
old_entries_by_ids = {e.unique_id: e for e in old_entries}
new_entries_by_ids = {e.unique_id: e for e in new_entries}
unique_id = "0000000000XX-00:00:00:00:00:03"
old_entry = old_entries_by_ids[unique_id]
new_entry = new_entries_by_ids[unique_id]
assert old_entry.original_name != new_entry.original_name
assert old_entry.original_icon != new_entry.original_icon