Improve test coverage of the homeworks integration (#122865)

* Improve test coverage of the homeworks integration

* Revert changes from the future

* Revert changes from the future
pull/122867/head
Erik Montnemery 2024-07-30 18:40:36 +02:00 committed by GitHub
parent 50b35ac4bc
commit fb229fcae8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions

View File

@ -171,16 +171,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if not await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
return False
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
data: HomeworksData = hass.data[DOMAIN].pop(entry.entry_id)
for keypad in data.keypads.values():
keypad.unsubscribe()
data: HomeworksData = hass.data[DOMAIN].pop(entry.entry_id)
for keypad in data.keypads.values():
keypad.unsubscribe()
await hass.async_add_executor_job(data.controller.close)
await hass.async_add_executor_job(data.controller.close)
return True
return unload_ok
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:

View File

@ -8,6 +8,7 @@ import pytest
from homeassistant.components.homeworks import EVENT_BUTTON_PRESS, EVENT_BUTTON_RELEASE
from homeassistant.components.homeworks.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
@ -165,3 +166,25 @@ async def test_send_command(
blocking=True,
)
assert len(mock_controller._send.mock_calls) == 0
async def test_cleanup_on_ha_shutdown(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_homeworks: MagicMock,
) -> None:
"""Test cleanup when HA shuts down."""
mock_controller = MagicMock()
mock_homeworks.return_value = mock_controller
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
mock_homeworks.assert_called_once_with("192.168.0.1", 1234, ANY)
mock_controller.stop.assert_not_called()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
mock_controller.close.assert_called_once_with()