hass-core/tests/components/emulated_hue/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

138 lines
4.2 KiB
Python

"""Test the Emulated Hue component."""
from datetime import timedelta
from unittest.mock import AsyncMock, patch
from homeassistant.components.emulated_hue.config import (
DATA_KEY,
DATA_VERSION,
SAVE_DELAY,
Config,
)
from homeassistant.components.emulated_hue.upnp import UPNPResponderProtocol
from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.setup import async_setup_component
from homeassistant.util import utcnow
from tests.common import async_fire_time_changed
async def test_config_google_home_entity_id_to_number(hass, hass_storage):
"""Test config adheres to the type."""
conf = Config(hass, {"type": "google_home"}, "127.0.0.1")
hass_storage[DATA_KEY] = {
"version": DATA_VERSION,
"key": DATA_KEY,
"data": {"1": "light.test2"},
}
await conf.async_setup()
number = conf.entity_id_to_number("light.test")
assert number == "2"
async_fire_time_changed(hass, utcnow() + timedelta(seconds=SAVE_DELAY))
await hass.async_block_till_done()
assert hass_storage[DATA_KEY]["data"] == {
"1": "light.test2",
"2": "light.test",
}
number = conf.entity_id_to_number("light.test")
assert number == "2"
number = conf.entity_id_to_number("light.test2")
assert number == "1"
entity_id = conf.number_to_entity_id("1")
assert entity_id == "light.test2"
async def test_config_google_home_entity_id_to_number_altered(hass, hass_storage):
"""Test config adheres to the type."""
conf = Config(hass, {"type": "google_home"}, "127.0.0.1")
hass_storage[DATA_KEY] = {
"version": DATA_VERSION,
"key": DATA_KEY,
"data": {"21": "light.test2"},
}
await conf.async_setup()
number = conf.entity_id_to_number("light.test")
assert number == "22"
async_fire_time_changed(hass, utcnow() + timedelta(seconds=SAVE_DELAY))
await hass.async_block_till_done()
assert hass_storage[DATA_KEY]["data"] == {
"21": "light.test2",
"22": "light.test",
}
number = conf.entity_id_to_number("light.test")
assert number == "22"
number = conf.entity_id_to_number("light.test2")
assert number == "21"
entity_id = conf.number_to_entity_id("21")
assert entity_id == "light.test2"
async def test_config_google_home_entity_id_to_number_empty(hass, hass_storage):
"""Test config adheres to the type."""
conf = Config(hass, {"type": "google_home"}, "127.0.0.1")
hass_storage[DATA_KEY] = {"version": DATA_VERSION, "key": DATA_KEY, "data": {}}
await conf.async_setup()
number = conf.entity_id_to_number("light.test")
assert number == "1"
async_fire_time_changed(hass, utcnow() + timedelta(seconds=SAVE_DELAY))
await hass.async_block_till_done()
assert hass_storage[DATA_KEY]["data"] == {"1": "light.test"}
number = conf.entity_id_to_number("light.test")
assert number == "1"
number = conf.entity_id_to_number("light.test2")
assert number == "2"
entity_id = conf.number_to_entity_id("2")
assert entity_id == "light.test2"
def test_config_alexa_entity_id_to_number():
"""Test config adheres to the type."""
conf = Config(None, {"type": "alexa"}, "127.0.0.1")
number = conf.entity_id_to_number("light.test")
assert number == "light.test"
number = conf.entity_id_to_number("light.test")
assert number == "light.test"
number = conf.entity_id_to_number("light.test2")
assert number == "light.test2"
entity_id = conf.number_to_entity_id("light.test")
assert entity_id == "light.test"
async def test_setup_works(hass):
"""Test setup works."""
hass.config.components.add("network")
with patch(
"homeassistant.components.emulated_hue.async_create_upnp_datagram_endpoint",
AsyncMock(),
) as mock_create_upnp_datagram_endpoint, patch(
"homeassistant.components.emulated_hue.async_get_source_ip"
):
mock_create_upnp_datagram_endpoint.return_value = AsyncMock(
spec=UPNPResponderProtocol
)
assert await async_setup_component(hass, "emulated_hue", {})
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
assert len(mock_create_upnp_datagram_endpoint.mock_calls) == 1