hass-core/tests/components/modem_callerid/test_init.py
Joakim Plate 8bd4125390
Test corrections noticed during pytest upgrade (#82579)
* Withing trigger a call to coordinator after init

* Stop discovery task on STOP event

* Stop dsmr connection task on STOP

* Use autospec in modem_serial tests

close on AioSerial is non async, and should not be mocked as a coroutine

* Make sure responder is specced correctly

tests/components/emulated_hue/test_init.py::test_setup_works
  /Users/joakim/src/hass/home-assistant/homeassistant/components/emulated_hue/__init__.py:119: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
    protocol.close()

* Don't assume Pymodbus is the only thing logging

* Use gather instead of wait

```
homeassistant/components/homeassistant/__init__.py:208: DeprecationWarning: The explicit passing of coroutine objects to asyncio.wait() is deprecated since Python 3.8, and scheduled for removal in Python 3.11.
```

* Increase wait time for automation tests

Assuming exit within 1 seconds is unreasonable on a potentially loaded
test machine.

* Avoid changing app state after startup
2022-11-23 20:53:28 +01:00

63 lines
2.1 KiB
Python

"""Test Modem Caller ID integration."""
from unittest.mock import patch
from phone_modem import exceptions
from homeassistant.components.modem_callerid.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_DEVICE
from homeassistant.core import HomeAssistant
from . import com_port, patch_init_modem
from tests.common import MockConfigEntry
async def test_setup_entry(hass: HomeAssistant):
"""Test Modem Caller ID entry setup."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_DEVICE: com_port().device},
)
entry.add_to_hass(hass)
with patch("aioserial.AioSerial", autospec=True), patch(
"homeassistant.components.modem_callerid.PhoneModem._get_response",
return_value="OK",
), patch("phone_modem.PhoneModem._modem_sm"):
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.LOADED
async def test_async_setup_entry_not_ready(hass: HomeAssistant):
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_DEVICE: com_port().device},
)
entry.add_to_hass(hass)
with patch_init_modem() as modemmock:
modemmock.side_effect = exceptions.SerialError
await hass.config_entries.async_setup(entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
async def test_unload_entry(hass: HomeAssistant):
"""Test unload."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_DEVICE: com_port().device},
)
entry.add_to_hass(hass)
with patch_init_modem():
await hass.config_entries.async_setup(entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN)