Close androidtv ADB socket connection when Home Assistant stops (#37973)
* Close the ADB connection on HA stop * Get the test to pass * Remove unnecessary test code * Register the callback sooner * '_async_stop' -> 'async_close' * 'async_close' method -> '_async_close' function
This commit is contained in:
parent
967a168ab7
commit
2c3618e2c7
2 changed files with 28 additions and 0 deletions
|
@ -40,6 +40,7 @@ from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
STATE_IDLE,
|
STATE_IDLE,
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_PAUSED,
|
STATE_PAUSED,
|
||||||
|
@ -230,6 +231,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||||
)
|
)
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
|
async def _async_close(event):
|
||||||
|
"""Close the ADB socket connection when HA stops."""
|
||||||
|
await aftv.adb_close()
|
||||||
|
|
||||||
|
# Close the ADB connection when HA stops
|
||||||
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_close)
|
||||||
|
|
||||||
device_args = [
|
device_args = [
|
||||||
aftv,
|
aftv,
|
||||||
config[CONF_NAME],
|
config[CONF_NAME],
|
||||||
|
|
|
@ -47,6 +47,7 @@ from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_PLAYING,
|
STATE_PLAYING,
|
||||||
STATE_STANDBY,
|
STATE_STANDBY,
|
||||||
|
@ -1154,3 +1155,22 @@ async def test_services_firetv(hass):
|
||||||
await _test_service(hass, entity_id, SERVICE_MEDIA_STOP, "back")
|
await _test_service(hass, entity_id, SERVICE_MEDIA_STOP, "back")
|
||||||
await _test_service(hass, entity_id, SERVICE_TURN_OFF, "adb_shell")
|
await _test_service(hass, entity_id, SERVICE_TURN_OFF, "adb_shell")
|
||||||
await _test_service(hass, entity_id, SERVICE_TURN_ON, "adb_shell")
|
await _test_service(hass, entity_id, SERVICE_TURN_ON, "adb_shell")
|
||||||
|
|
||||||
|
|
||||||
|
async def test_connection_closed_on_ha_stop(hass):
|
||||||
|
"""Test that the ADB socket connection is closed when HA stops."""
|
||||||
|
patch_key, entity_id = _setup(CONFIG_ANDROIDTV_ADB_SERVER)
|
||||||
|
|
||||||
|
with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[patch_key]:
|
||||||
|
with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]:
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass, DOMAIN, CONFIG_ANDROIDTV_ADB_SERVER
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"androidtv.androidtv.androidtv_async.AndroidTVAsync.adb_close"
|
||||||
|
) as adb_close:
|
||||||
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert adb_close.called
|
||||||
|
|
Loading…
Add table
Reference in a new issue