core/tests/components/schlage/test_lock.py
J. Nick Koston 9a79320861
Mark executor jobs as background unless created from a tracked task (#114450)
* Mark executor jobs as background unless created from a tracked task

If the current task is not tracked the executor job should not
be a background task to avoid delaying startup and shutdown.

Currently any executor job created in a untracked task or
background task would end up being tracked and delaying
startup/shutdown

* import exec has the same issue

* Avoid tracking import executor jobs

There is no reason to track these jobs as they are always awaited
and we do not want to support fire and forget import executor jobs

* fix xiaomi_miio

* lots of fire time changed without background await

* revert changes moved to other PR

* more

* more

* more

* m

* m

* p

* fix fire and forget tests

* scrape

* sonos

* system

* more

* capture callback before block

* coverage

* more

* more races

* more races

* more

* missed some

* more fixes

* missed some more

* fix

* remove unneeded

* one more race

* two
2024-03-30 00:16:53 -04:00

68 lines
2.3 KiB
Python

"""Test schlage lock."""
from datetime import timedelta
from unittest.mock import Mock
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_LOCK, SERVICE_UNLOCK
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed
async def test_lock_device_registry(
hass: HomeAssistant, mock_added_config_entry: ConfigEntry
) -> None:
"""Test lock is added to device registry."""
device_registry = dr.async_get(hass)
device = device_registry.async_get_device(identifiers={("schlage", "test")})
assert device.model == "<model-name>"
assert device.sw_version == "1.0"
assert device.name == "Vault Door"
assert device.manufacturer == "Schlage"
async def test_lock_services(
hass: HomeAssistant, mock_lock: Mock, mock_added_config_entry: ConfigEntry
) -> None:
"""Test lock services."""
await hass.services.async_call(
LOCK_DOMAIN,
SERVICE_LOCK,
service_data={ATTR_ENTITY_ID: "lock.vault_door"},
blocking=True,
)
await hass.async_block_till_done()
mock_lock.lock.assert_called_once_with()
await hass.services.async_call(
LOCK_DOMAIN,
SERVICE_UNLOCK,
service_data={ATTR_ENTITY_ID: "lock.vault_door"},
blocking=True,
)
await hass.async_block_till_done()
mock_lock.unlock.assert_called_once_with()
await hass.config_entries.async_unload(mock_added_config_entry.entry_id)
async def test_changed_by(
hass: HomeAssistant, mock_lock: Mock, mock_added_config_entry: ConfigEntry
) -> None:
"""Test population of the changed_by attribute."""
mock_lock.last_changed_by.reset_mock()
mock_lock.last_changed_by.return_value = "access code - foo"
# Make the coordinator refresh data.
async_fire_time_changed(hass, utcnow() + timedelta(seconds=31))
await hass.async_block_till_done(wait_background_tasks=True)
mock_lock.last_changed_by.assert_called_once_with()
lock_device = hass.states.get("lock.vault_door")
assert lock_device is not None
assert lock_device.attributes.get("changed_by") == "access code - foo"