Discover new added device at runtime in AVM Fritz!Smarthome (#103859)
This commit is contained in:
parent
923c13907c
commit
9c5e0fc2c9
17 changed files with 362 additions and 109 deletions
|
@ -45,6 +45,17 @@ async def setup_config_entry(
|
|||
return result
|
||||
|
||||
|
||||
def set_devices(
|
||||
fritz: Mock, devices: list[Mock] | None = None, templates: list[Mock] | None = None
|
||||
) -> None:
|
||||
"""Set list of devices or templates."""
|
||||
if devices is not None:
|
||||
fritz().get_devices.return_value = devices
|
||||
|
||||
if templates is not None:
|
||||
fritz().get_templates.return_value = templates
|
||||
|
||||
|
||||
class FritzEntityBaseMock(Mock):
|
||||
"""base mock of a AVM Fritz!Box binary sensor device."""
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceBinarySensorMock, setup_config_entry
|
||||
from . import FritzDeviceBinarySensorMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -126,3 +126,26 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
|
||||
assert fritz().update_devices.call_count == 2
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceBinarySensorMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{ENTITY_ID}_alarm")
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceBinarySensorMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_device"
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_device_alarm")
|
||||
assert state
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
"""Tests for AVM Fritz!Box templates."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock
|
||||
|
||||
from homeassistant.components.button import DOMAIN, SERVICE_PRESS
|
||||
|
@ -10,10 +11,13 @@ from homeassistant.const import (
|
|||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzEntityBaseMock, setup_config_entry
|
||||
from . import FritzEntityBaseMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.{CONF_FAKE_NAME}"
|
||||
|
||||
|
||||
|
@ -41,3 +45,26 @@ async def test_apply_template(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert fritz().apply_template.call_count == 1
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
template = FritzEntityBaseMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], fritz=fritz, template=template
|
||||
)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
|
||||
new_template = FritzEntityBaseMock()
|
||||
new_template.ain = "7890 1234"
|
||||
new_template.name = "new_template"
|
||||
set_devices(fritz, templates=[template, new_template])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_template")
|
||||
assert state
|
||||
|
|
|
@ -41,7 +41,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceClimateMock, setup_config_entry
|
||||
from . import FritzDeviceClimateMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -402,3 +402,26 @@ async def test_preset_mode_update(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
assert fritz().update_devices.call_count == 3
|
||||
assert state
|
||||
assert state.attributes[ATTR_PRESET_MODE] == PRESET_ECO
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceClimateMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceClimateMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_climate"
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_climate")
|
||||
assert state
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
"""Tests for AVM Fritz!Box switch component."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock, call
|
||||
|
||||
from homeassistant.components.cover import ATTR_CURRENT_POSITION, ATTR_POSITION, DOMAIN
|
||||
|
@ -12,10 +13,13 @@ from homeassistant.const import (
|
|||
SERVICE_STOP_COVER,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceCoverMock, setup_config_entry
|
||||
from . import FritzDeviceCoverMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.{CONF_FAKE_NAME}"
|
||||
|
||||
|
||||
|
@ -84,3 +88,26 @@ async def test_stop_cover(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
DOMAIN, SERVICE_STOP_COVER, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert device.set_blind_stop.call_count == 1
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceCoverMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceCoverMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_climate"
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_climate")
|
||||
assert state
|
||||
|
|
|
@ -29,7 +29,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceLightMock, setup_config_entry
|
||||
from . import FritzDeviceLightMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -262,3 +262,38 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
|
||||
assert fritz().update_devices.call_count == 4
|
||||
assert fritz().login.call_count == 4
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceLightMock()
|
||||
device.get_color_temps.return_value = [2700, 6500]
|
||||
device.get_colors.return_value = {
|
||||
"Red": [("100", "70", "10"), ("100", "50", "10"), ("100", "30", "10")]
|
||||
}
|
||||
device.color_mode = COLOR_TEMP_MODE
|
||||
device.color_temp = 2700
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceLightMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_light"
|
||||
new_device.get_color_temps.return_value = [2700, 6500]
|
||||
new_device.get_colors.return_value = {
|
||||
"Red": [("100", "70", "10"), ("100", "50", "10"), ("100", "30", "10")]
|
||||
}
|
||||
new_device.color_mode = COLOR_TEMP_MODE
|
||||
new_device.color_temp = 2700
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_light")
|
||||
assert state
|
||||
|
|
|
@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers import entity_registry as er
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceSensorMock, setup_config_entry
|
||||
from . import FritzDeviceSensorMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -108,3 +108,26 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock) -> None:
|
|||
|
||||
assert fritz().update_devices.call_count == 4
|
||||
assert fritz().login.call_count == 4
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceSensorMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{ENTITY_ID}_temperature")
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceSensorMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_device"
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_device_temperature")
|
||||
assert state
|
||||
|
|
|
@ -31,7 +31,7 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers import entity_registry as er
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import FritzDeviceSwitchMock, setup_config_entry
|
||||
from . import FritzDeviceSwitchMock, set_devices, setup_config_entry
|
||||
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -187,3 +187,26 @@ async def test_assume_device_unavailable(hass: HomeAssistant, fritz: Mock) -> No
|
|||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_discover_new_device(hass: HomeAssistant, fritz: Mock) -> None:
|
||||
"""Test adding new discovered devices during runtime."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
assert await setup_config_entry(
|
||||
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
||||
)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
|
||||
new_device = FritzDeviceSwitchMock()
|
||||
new_device.ain = "7890 1234"
|
||||
new_device.name = "new_switch"
|
||||
set_devices(fritz, devices=[device, new_device])
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.new_switch")
|
||||
assert state
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue