Skip to content

Commit

Permalink
Update i-j* tests to use entity & device registry fixtures (#103900)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Nov 13, 2023
1 parent be2cee2 commit 92b3c0c
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 151 deletions.
5 changes: 3 additions & 2 deletions tests/components/ibeacon/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ async def remove_device(ws_client, device_id, config_entry_id):


async def test_device_remove_devices(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test we can only remove a device that no longer exists."""
entry = MockConfigEntry(
Expand All @@ -46,7 +48,6 @@ async def test_device_remove_devices(
await hass.async_block_till_done()
inject_bluetooth_service_info(hass, BLUECHARM_BEACON_SERVICE_INFO)
await hass.async_block_till_done()
device_registry = dr.async_get(hass)

device_entry = device_registry.async_get_device(
identifiers={
Expand Down
43 changes: 25 additions & 18 deletions tests/components/input_boolean/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ async def test_input_boolean_context(
assert state2.context.user_id == hass_admin_user.id


async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
async def test_reload(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hass_admin_user: MockUser
) -> None:
"""Test reload service."""
count_start = len(hass.states.async_entity_ids())
ent_reg = er.async_get(hass)

_LOGGER.debug("ENTITIES @ start: %s", hass.states.async_entity_ids())

Expand Down Expand Up @@ -226,9 +227,9 @@ async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
assert state_3 is None
assert state_2.state == STATE_ON

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is None

with patch(
"homeassistant.config.load_yaml_config_file",
Expand Down Expand Up @@ -261,9 +262,9 @@ async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
assert state_2 is not None
assert state_3 is not None

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is not None

assert state_2.state == STATE_ON # reload is not supposed to change entity state
assert state_2.attributes.get(ATTR_FRIENDLY_NAME) == "Hello World reloaded"
Expand Down Expand Up @@ -316,18 +317,20 @@ async def test_ws_list(


async def test_ws_delete(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test WS delete cleans up entity registry."""
assert await storage_setup()

input_id = "from_storage"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None

client = await hass_ws_client(hass)

Expand All @@ -339,11 +342,14 @@ async def test_ws_delete(

state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None


async def test_ws_update(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test update WS."""

Expand All @@ -355,12 +361,11 @@ async def test_ws_update(

input_id = "from_storage"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is not None
assert state.state
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None

client = await hass_ws_client(hass)

Expand Down Expand Up @@ -400,18 +405,20 @@ async def test_ws_update(


async def test_ws_create(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test create WS."""
assert await storage_setup(items=[])

input_id = "new_input"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None

client = await hass_ws_client(hass)

Expand Down
37 changes: 21 additions & 16 deletions tests/components/input_button/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ async def test_input_button_context(
assert state2.context.user_id == hass_admin_user.id


async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
async def test_reload(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hass_admin_user: MockUser
) -> None:
"""Test reload service."""
count_start = len(hass.states.async_entity_ids())
ent_reg = er.async_get(hass)

_LOGGER.debug("ENTITIES @ start: %s", hass.states.async_entity_ids())

Expand All @@ -163,9 +164,9 @@ async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
assert state_2 is not None
assert state_3 is None

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is None

with patch(
"homeassistant.config.load_yaml_config_file",
Expand Down Expand Up @@ -197,9 +198,9 @@ async def test_reload(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
assert state_2 is not None
assert state_3 is not None

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_1") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_2") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "test_3") is not None


async def test_reload_not_changing_state(hass: HomeAssistant, storage_setup) -> None:
Expand Down Expand Up @@ -288,7 +289,10 @@ async def test_ws_list(


async def test_ws_create_update(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test creating and updating via WS."""
assert await storage_setup(config={DOMAIN: {}})
Expand All @@ -304,8 +308,7 @@ async def test_ws_create_update(
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "new"

ent_reg = er.async_get(hass)
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "new") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "new") is not None

await client.send_json(
{"id": 8, "type": f"{DOMAIN}/update", f"{DOMAIN}_id": "new", "name": "newer"}
Expand All @@ -319,22 +322,24 @@ async def test_ws_create_update(
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "newer"

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "new") is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "new") is not None


async def test_ws_delete(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test WS delete cleans up entity registry."""
assert await storage_setup()

input_id = "from_storage"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None

client = await hass_ws_client(hass)

Expand All @@ -346,7 +351,7 @@ async def test_ws_delete(

state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None


async def test_setup_no_config(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
Expand Down
48 changes: 30 additions & 18 deletions tests/components/input_datetime/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,13 @@ async def test_input_datetime_context(


async def test_reload(
hass: HomeAssistant, hass_admin_user: MockUser, hass_read_only_user: MockUser
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_admin_user: MockUser,
hass_read_only_user: MockUser,
) -> None:
"""Test reload service."""
count_start = len(hass.states.async_entity_ids())
ent_reg = er.async_get(hass)

assert await async_setup_component(
hass,
Expand All @@ -451,9 +453,9 @@ async def test_reload(
assert state_2 is None
assert state_3 is not None
assert dt_obj.strftime(FORMAT_DATE) == state_1.state
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt3") == f"{DOMAIN}.dt3"
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt2") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt3") == f"{DOMAIN}.dt3"

with patch(
"homeassistant.config.load_yaml_config_file",
Expand Down Expand Up @@ -493,9 +495,9 @@ async def test_reload(
datetime.date.today(), DEFAULT_TIME
).strftime(FORMAT_DATETIME)

assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") == f"{DOMAIN}.dt2"
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt3") is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt2") == f"{DOMAIN}.dt2"
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, "dt3") is None


async def test_load_from_storage(hass: HomeAssistant, storage_setup) -> None:
Expand Down Expand Up @@ -553,18 +555,22 @@ async def test_ws_list(


async def test_ws_delete(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test WS delete cleans up entity registry."""
assert await storage_setup()

input_id = "from_storage"
input_entity_id = f"{DOMAIN}.datetime_from_storage"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) == input_entity_id
assert (
entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) == input_entity_id
)

client = await hass_ws_client(hass)

Expand All @@ -576,24 +582,28 @@ async def test_ws_delete(

state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None


async def test_update(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test updating min/max updates the state."""

assert await storage_setup()

input_id = "from_storage"
input_entity_id = f"{DOMAIN}.datetime_from_storage"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state.attributes[ATTR_FRIENDLY_NAME] == "datetime from storage"
assert state.state == INITIAL_DATETIME
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) == input_entity_id
assert (
entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) == input_entity_id
)

client = await hass_ws_client(hass)

Expand Down Expand Up @@ -621,18 +631,20 @@ async def test_update(


async def test_ws_create(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
storage_setup,
) -> None:
"""Test create WS."""
assert await storage_setup(items=[])

input_id = "new_datetime"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)

state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None

client = await hass_ws_client(hass)

Expand Down

0 comments on commit 92b3c0c

Please sign in to comment.