Add config flow and device registry to fritzbox integration (#31240)
* add config flow * fix pylint * update lib * Update config_flow.py * remote devices layer in config * add default host * avoid double setups of entities * remove async_setup_platform * store entities in hass.data * pass fritz connection together with config_entry * fritz connections try no4 (or is it even more) * fix comments * add unloading * fixed comments * Update config_flow.py * Update const.py * Update config_flow.py * Update __init__.py * Update config_flow.py * Update __init__.py * Update __init__.py * Update config_flow.py * Update __init__.py * Update __init__.py * Update __init__.py * Update config_flow.py * add init tests * test unloading * add switch tests * add sensor tests * add climate tests * test target temperature * mock config to package * comments * test binary sensor state * add config flow tests * comments * add missing tests * minor * remove string title * deprecate yaml * don't change yaml * get devices async * minor * add devices again * comments fixed * unique_id fixes * fix patches * Fix schema Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
2123f6f133
commit
c87ecf0ff6
22 files changed
+1403
-271
No files matched your search
@@ -1 +1,99 @@
|
||||
"""Tests for the FritzBox! integration."""
|
||||
"""Tests for the AVM Fritz!Box integration."""
|
||||
from unittest.mock import Mock
|
||||
|
||||
from homeassistant.components.fritzbox.const import DOMAIN
|
||||
from homeassistant.const import CONF_DEVICES, CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
|
||||
MOCK_CONFIG = {
|
||||
DOMAIN: {
|
||||
CONF_DEVICES: [
|
||||
{
|
||||
CONF_HOST: "fake_host",
|
||||
CONF_PASSWORD: "fake_pass",
|
||||
CONF_USERNAME: "fake_user",
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class FritzDeviceBinarySensorMock(Mock):
|
||||
"""Mock of a AVM Fritz!Box binary sensor device."""
|
||||
|
||||
ain = "fake_ain"
|
||||
alert_state = "fake_state"
|
||||
fw_version = "1.2.3"
|
||||
has_alarm = True
|
||||
has_switch = False
|
||||
has_temperature_sensor = False
|
||||
has_thermostat = False
|
||||
manufacturer = "fake_manufacturer"
|
||||
name = "fake_name"
|
||||
present = True
|
||||
productname = "fake_productname"
|
||||
|
||||
|
||||
class FritzDeviceClimateMock(Mock):
|
||||
"""Mock of a AVM Fritz!Box climate device."""
|
||||
|
||||
actual_temperature = 18.0
|
||||
ain = "fake_ain"
|
||||
alert_state = "fake_state"
|
||||
battery_level = 23
|
||||
battery_low = True
|
||||
comfort_temperature = 22.0
|
||||
device_lock = "fake_locked_device"
|
||||
eco_temperature = 16.0
|
||||
fw_version = "1.2.3"
|
||||
has_alarm = False
|
||||
has_switch = False
|
||||
has_temperature_sensor = False
|
||||
has_thermostat = True
|
||||
holiday_active = "fake_holiday"
|
||||
lock = "fake_locked"
|
||||
manufacturer = "fake_manufacturer"
|
||||
name = "fake_name"
|
||||
present = True
|
||||
productname = "fake_productname"
|
||||
summer_active = "fake_summer"
|
||||
target_temperature = 19.5
|
||||
window_open = "fake_window"
|
||||
|
||||
|
||||
class FritzDeviceSensorMock(Mock):
|
||||
"""Mock of a AVM Fritz!Box sensor device."""
|
||||
|
||||
ain = "fake_ain"
|
||||
device_lock = "fake_locked_device"
|
||||
fw_version = "1.2.3"
|
||||
has_alarm = False
|
||||
has_switch = False
|
||||
has_temperature_sensor = True
|
||||
has_thermostat = False
|
||||
lock = "fake_locked"
|
||||
manufacturer = "fake_manufacturer"
|
||||
name = "fake_name"
|
||||
present = True
|
||||
productname = "fake_productname"
|
||||
temperature = 1.23
|
||||
|
||||
|
||||
class FritzDeviceSwitchMock(Mock):
|
||||
"""Mock of a AVM Fritz!Box switch device."""
|
||||
|
||||
ain = "fake_ain"
|
||||
device_lock = "fake_locked_device"
|
||||
energy = 1234
|
||||
fw_version = "1.2.3"
|
||||
has_alarm = False
|
||||
has_switch = True
|
||||
has_temperature_sensor = True
|
||||
has_thermostat = False
|
||||
switch_state = "fake_state"
|
||||
lock = "fake_locked"
|
||||
manufacturer = "fake_manufacturer"
|
||||
name = "fake_name"
|
||||
power = 5678
|
||||
present = True
|
||||
productname = "fake_productname"
|
||||
temperature = 135
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Fixtures for the AVM Fritz!Box integration."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(name="fritz")
|
||||
def fritz_fixture() -> Mock:
|
||||
"""Patch libraries."""
|
||||
with patch("homeassistant.components.fritzbox.socket") as socket, patch(
|
||||
"homeassistant.components.fritzbox.Fritzhome"
|
||||
) as fritz, patch("homeassistant.components.fritzbox.config_flow.Fritzhome"):
|
||||
socket.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
yield fritz
|
||||
@@ -0,0 +1,94 @@
|
||||
"""Tests for AVM Fritz!Box binary sensor component."""
|
||||
from datetime import timedelta
|
||||
from unittest import mock
|
||||
from unittest.mock import Mock
|
||||
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN
|
||||
from homeassistant.components.fritzbox.const import DOMAIN as FB_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import MOCK_CONFIG, FritzDeviceBinarySensorMock
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.fake_name"
|
||||
|
||||
|
||||
async def setup_fritzbox(hass: HomeAssistantType, config: dict):
|
||||
"""Set up mock AVM Fritz!Box."""
|
||||
assert await async_setup_component(hass, FB_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setup of platform."""
|
||||
device = FritzDeviceBinarySensorMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
assert state
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake_name"
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] == "window"
|
||||
|
||||
|
||||
async def test_is_off(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test state of platform."""
|
||||
device = FritzDeviceBinarySensorMock()
|
||||
device.present = False
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
assert state
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_update(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceBinarySensorMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 2
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
|
||||
async def test_update_error(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceBinarySensorMock()
|
||||
device.update.side_effect = [mock.DEFAULT, HTTPError("Boom")]
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 2
|
||||
assert fritz().login.call_count == 2
|
||||
@@ -1,151 +1,306 @@
|
||||
"""The tests for the demo climate component."""
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
"""Tests for AVM Fritz!Box climate component."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock, call
|
||||
|
||||
import requests
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from homeassistant.components.fritzbox.climate import FritzboxThermostat
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.components.climate.const import (
|
||||
ATTR_CURRENT_TEMPERATURE,
|
||||
ATTR_HVAC_MODE,
|
||||
ATTR_HVAC_MODES,
|
||||
ATTR_MAX_TEMP,
|
||||
ATTR_MIN_TEMP,
|
||||
ATTR_PRESET_MODE,
|
||||
ATTR_PRESET_MODES,
|
||||
DOMAIN,
|
||||
HVAC_MODE_HEAT,
|
||||
HVAC_MODE_OFF,
|
||||
PRESET_COMFORT,
|
||||
PRESET_ECO,
|
||||
SERVICE_SET_HVAC_MODE,
|
||||
SERVICE_SET_PRESET_MODE,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
)
|
||||
from homeassistant.components.fritzbox.const import (
|
||||
ATTR_STATE_BATTERY_LOW,
|
||||
ATTR_STATE_DEVICE_LOCKED,
|
||||
ATTR_STATE_HOLIDAY_MODE,
|
||||
ATTR_STATE_LOCKED,
|
||||
ATTR_STATE_SUMMER_MODE,
|
||||
ATTR_STATE_WINDOW_OPEN,
|
||||
DOMAIN as FB_DOMAIN,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_BATTERY_LEVEL,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_TEMPERATURE,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import MOCK_CONFIG, FritzDeviceClimateMock
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.fake_name"
|
||||
|
||||
|
||||
class TestFritzboxClimate(unittest.TestCase):
|
||||
"""Test Fritz!Box heating thermostats."""
|
||||
async def setup_fritzbox(hass: HomeAssistantType, config: dict):
|
||||
"""Set up mock AVM Fritz!Box."""
|
||||
assert await async_setup_component(hass, FB_DOMAIN, config) is True
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def setUp(self):
|
||||
"""Create a mock device to test on."""
|
||||
self.device = Mock()
|
||||
self.device.name = "Test Thermostat"
|
||||
self.device.actual_temperature = 18.0
|
||||
self.device.target_temperature = 19.5
|
||||
self.device.comfort_temperature = 22.0
|
||||
self.device.eco_temperature = 16.0
|
||||
self.device.present = True
|
||||
self.device.device_lock = True
|
||||
self.device.lock = False
|
||||
self.device.battery_low = True
|
||||
self.device.set_target_temperature = Mock()
|
||||
self.device.update = Mock()
|
||||
mock_fritz = Mock()
|
||||
mock_fritz.login = Mock()
|
||||
self.thermostat = FritzboxThermostat(self.device, mock_fritz)
|
||||
|
||||
def test_init(self):
|
||||
"""Test instance creation."""
|
||||
assert 18.0 == self.thermostat._current_temperature
|
||||
assert 19.5 == self.thermostat._target_temperature
|
||||
assert 22.0 == self.thermostat._comfort_temperature
|
||||
assert 16.0 == self.thermostat._eco_temperature
|
||||
async def test_setup(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setup of platform."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
def test_supported_features(self):
|
||||
"""Test supported features property."""
|
||||
assert self.thermostat.supported_features == 17
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
def test_available(self):
|
||||
"""Test available property."""
|
||||
assert self.thermostat.available
|
||||
self.thermostat._device.present = False
|
||||
assert not self.thermostat.available
|
||||
assert state
|
||||
assert state.attributes[ATTR_BATTERY_LEVEL] == 23
|
||||
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 18
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake_name"
|
||||
assert state.attributes[ATTR_HVAC_MODES] == [HVAC_MODE_HEAT, HVAC_MODE_OFF]
|
||||
assert state.attributes[ATTR_MAX_TEMP] == 28
|
||||
assert state.attributes[ATTR_MIN_TEMP] == 8
|
||||
assert state.attributes[ATTR_PRESET_MODE] is None
|
||||
assert state.attributes[ATTR_PRESET_MODES] == [PRESET_ECO, PRESET_COMFORT]
|
||||
assert state.attributes[ATTR_STATE_BATTERY_LOW] is True
|
||||
assert state.attributes[ATTR_STATE_DEVICE_LOCKED] == "fake_locked_device"
|
||||
assert state.attributes[ATTR_STATE_HOLIDAY_MODE] == "fake_holiday"
|
||||
assert state.attributes[ATTR_STATE_LOCKED] == "fake_locked"
|
||||
assert state.attributes[ATTR_STATE_SUMMER_MODE] == "fake_summer"
|
||||
assert state.attributes[ATTR_STATE_WINDOW_OPEN] == "fake_window"
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 19.5
|
||||
assert state.state == HVAC_MODE_HEAT
|
||||
|
||||
def test_name(self):
|
||||
"""Test name property."""
|
||||
assert "Test Thermostat" == self.thermostat.name
|
||||
|
||||
def test_temperature_unit(self):
|
||||
"""Test temperature_unit property."""
|
||||
assert TEMP_CELSIUS == self.thermostat.temperature_unit
|
||||
async def test_target_temperature_on(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test turn device on."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
device.target_temperature = 127.0
|
||||
|
||||
def test_precision(self):
|
||||
"""Test precision property."""
|
||||
assert 0.5 == self.thermostat.precision
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 30
|
||||
|
||||
def test_current_temperature(self):
|
||||
"""Test current_temperature property incl. special temperatures."""
|
||||
assert 18 == self.thermostat.current_temperature
|
||||
|
||||
def test_target_temperature(self):
|
||||
"""Test target_temperature property."""
|
||||
assert 19.5 == self.thermostat.target_temperature
|
||||
async def test_target_temperature_off(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test turn device on."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
device.target_temperature = 126.5
|
||||
|
||||
self.thermostat._target_temperature = 126.5
|
||||
assert self.thermostat.target_temperature == 0.0
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 0
|
||||
|
||||
self.thermostat._target_temperature = 127.0
|
||||
assert self.thermostat.target_temperature == 30.0
|
||||
|
||||
@patch.object(FritzboxThermostat, "set_hvac_mode")
|
||||
def test_set_temperature_operation_mode(self, mock_set_op):
|
||||
"""Test set_temperature by operation_mode."""
|
||||
self.thermostat.set_temperature(hvac_mode="heat")
|
||||
mock_set_op.assert_called_once_with("heat")
|
||||
async def test_update(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
def test_set_temperature_temperature(self):
|
||||
"""Test set_temperature by temperature."""
|
||||
self.thermostat.set_temperature(temperature=23.0)
|
||||
self.thermostat._device.set_target_temperature.assert_called_once_with(23.0)
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
@patch.object(FritzboxThermostat, "set_hvac_mode")
|
||||
def test_set_temperature_none(self, mock_set_op):
|
||||
"""Test set_temperature with no arguments."""
|
||||
self.thermostat.set_temperature()
|
||||
mock_set_op.assert_not_called()
|
||||
self.thermostat._device.set_target_temperature.assert_not_called()
|
||||
assert state
|
||||
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 18
|
||||
assert state.attributes[ATTR_MAX_TEMP] == 28
|
||||
assert state.attributes[ATTR_MIN_TEMP] == 8
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 19.5
|
||||
|
||||
@patch.object(FritzboxThermostat, "set_hvac_mode")
|
||||
def test_set_temperature_operation_mode_precedence(self, mock_set_op):
|
||||
"""Test set_temperature for precedence of operation_mode argument."""
|
||||
self.thermostat.set_temperature(hvac_mode="heat", temperature=23.0)
|
||||
mock_set_op.assert_called_once_with("heat")
|
||||
self.thermostat._device.set_target_temperature.assert_not_called()
|
||||
device.actual_temperature = 19
|
||||
device.target_temperature = 20
|
||||
|
||||
def test_hvac_mode(self):
|
||||
"""Test operation mode property for different temperatures."""
|
||||
self.thermostat._target_temperature = 127.0
|
||||
assert "heat" == self.thermostat.hvac_mode
|
||||
self.thermostat._target_temperature = 126.5
|
||||
assert "off" == self.thermostat.hvac_mode
|
||||
self.thermostat._target_temperature = 22.0
|
||||
assert "heat" == self.thermostat.hvac_mode
|
||||
self.thermostat._target_temperature = 16.0
|
||||
assert "heat" == self.thermostat.hvac_mode
|
||||
self.thermostat._target_temperature = 12.5
|
||||
assert "heat" == self.thermostat.hvac_mode
|
||||
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(ENTITY_ID)
|
||||
|
||||
def test_operation_list(self):
|
||||
"""Test operation_list property."""
|
||||
assert ["heat", "off"] == self.thermostat.hvac_modes
|
||||
assert device.update.call_count == 1
|
||||
assert state
|
||||
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 19
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 20
|
||||
|
||||
def test_min_max_temperature(self):
|
||||
"""Test min_temp and max_temp properties."""
|
||||
assert 8.0 == self.thermostat.min_temp
|
||||
assert 28.0 == self.thermostat.max_temp
|
||||
|
||||
def test_device_state_attributes(self):
|
||||
"""Test device_state property."""
|
||||
attr = self.thermostat.device_state_attributes
|
||||
assert attr["device_locked"] is True
|
||||
assert attr["locked"] is False
|
||||
assert attr["battery_low"] is True
|
||||
async def test_update_error(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceClimateMock()
|
||||
device.update.side_effect = HTTPError("Boom")
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
def test_update(self):
|
||||
"""Test update function."""
|
||||
device = Mock()
|
||||
device.update = Mock()
|
||||
device.actual_temperature = 10.0
|
||||
device.target_temperature = 11.0
|
||||
device.comfort_temperature = 12.0
|
||||
device.eco_temperature = 13.0
|
||||
self.thermostat._device = device
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
assert device.update.call_count == 0
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
self.thermostat.update()
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device.update.assert_called_once_with()
|
||||
assert 10.0 == self.thermostat._current_temperature
|
||||
assert 11.0 == self.thermostat._target_temperature
|
||||
assert 12.0 == self.thermostat._comfort_temperature
|
||||
assert 13.0 == self.thermostat._eco_temperature
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 2
|
||||
|
||||
def test_update_http_error(self):
|
||||
"""Test exception handling of update function."""
|
||||
self.device.update.side_effect = requests.exceptions.HTTPError
|
||||
self.thermostat.update()
|
||||
self.thermostat._fritz.login.assert_called_once_with()
|
||||
|
||||
async def test_set_temperature_temperature(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting temperature by temperature."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_TEMPERATURE: 123},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(123)]
|
||||
|
||||
|
||||
async def test_set_temperature_mode_off(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting temperature by mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
{
|
||||
ATTR_ENTITY_ID: ENTITY_ID,
|
||||
ATTR_HVAC_MODE: HVAC_MODE_OFF,
|
||||
ATTR_TEMPERATURE: 123,
|
||||
},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(0)]
|
||||
|
||||
|
||||
async def test_set_temperature_mode_heat(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting temperature by mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
{
|
||||
ATTR_ENTITY_ID: ENTITY_ID,
|
||||
ATTR_HVAC_MODE: HVAC_MODE_HEAT,
|
||||
ATTR_TEMPERATURE: 123,
|
||||
},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(22)]
|
||||
|
||||
|
||||
async def test_set_hvac_mode_off(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting hvac mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_HVAC_MODE,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVAC_MODE_OFF},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(0)]
|
||||
|
||||
|
||||
async def test_set_hvac_mode_heat(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting hvac mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_HVAC_MODE,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVAC_MODE_HEAT},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(22)]
|
||||
|
||||
|
||||
async def test_set_preset_mode_comfort(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting preset mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_PRESET_MODE,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: PRESET_COMFORT},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(22)]
|
||||
|
||||
|
||||
async def test_set_preset_mode_eco(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setting preset mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_PRESET_MODE,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: PRESET_ECO},
|
||||
True,
|
||||
)
|
||||
assert device.set_target_temperature.call_args_list == [call(16)]
|
||||
|
||||
|
||||
async def test_preset_mode_update(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test preset mode."""
|
||||
device = FritzDeviceClimateMock()
|
||||
device.comfort_temperature = 98
|
||||
device.eco_temperature = 99
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
assert state
|
||||
assert state.attributes[ATTR_PRESET_MODE] is None
|
||||
|
||||
device.target_temperature = 98
|
||||
|
||||
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(ENTITY_ID)
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert state
|
||||
assert state.attributes[ATTR_PRESET_MODE] == PRESET_COMFORT
|
||||
|
||||
device.target_temperature = 99
|
||||
|
||||
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(ENTITY_ID)
|
||||
|
||||
assert device.update.call_count == 2
|
||||
assert state
|
||||
assert state.attributes[ATTR_PRESET_MODE] == PRESET_ECO
|
||||
@@ -0,0 +1,179 @@
|
||||
"""Tests for AVM Fritz!Box config flow."""
|
||||
from unittest import mock
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from pyfritzhome import LoginError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.fritzbox.const import DOMAIN
|
||||
from homeassistant.components.ssdp import ATTR_SSDP_LOCATION, ATTR_UPNP_FRIENDLY_NAME
|
||||
from homeassistant.const import CONF_DEVICES, CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import MOCK_CONFIG
|
||||
|
||||
MOCK_USER_DATA = MOCK_CONFIG[DOMAIN][CONF_DEVICES][0]
|
||||
MOCK_SSDP_DATA = {
|
||||
ATTR_SSDP_LOCATION: "https://fake_host:12345/test",
|
||||
ATTR_UPNP_FRIENDLY_NAME: "fake_name",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="fritz")
|
||||
def fritz_fixture() -> Mock:
|
||||
"""Patch libraries."""
|
||||
with patch("homeassistant.components.fritzbox.config_flow.Fritzhome") as fritz:
|
||||
yield fritz
|
||||
|
||||
|
||||
async def test_user(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow by user."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "fake_host"
|
||||
assert result["data"][CONF_HOST] == "fake_host"
|
||||
assert result["data"][CONF_PASSWORD] == "fake_pass"
|
||||
assert result["data"][CONF_USERNAME] == "fake_user"
|
||||
|
||||
|
||||
async def test_user_auth_failed(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow by user with authentication failure."""
|
||||
fritz().login.side_effect = [LoginError("Boom"), mock.DEFAULT]
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"]["base"] == "auth_failed"
|
||||
|
||||
|
||||
async def test_user_not_successful(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow by user but no connection found."""
|
||||
fritz().login.side_effect = OSError("Boom")
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_found"
|
||||
|
||||
|
||||
async def test_user_already_configured(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow by user when already configured."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow by import."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "import"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "fake_host"
|
||||
assert result["data"][CONF_HOST] == "fake_host"
|
||||
assert result["data"][CONF_PASSWORD] == "fake_pass"
|
||||
assert result["data"][CONF_USERNAME] == "fake_user"
|
||||
|
||||
|
||||
async def test_ssdp(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow from discovery."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "fake_pass", CONF_USERNAME: "fake_user"},
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "fake_name"
|
||||
assert result["data"][CONF_HOST] == "fake_host"
|
||||
assert result["data"][CONF_PASSWORD] == "fake_pass"
|
||||
assert result["data"][CONF_USERNAME] == "fake_user"
|
||||
|
||||
|
||||
async def test_ssdp_auth_failed(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow from discovery with authentication failure."""
|
||||
fritz().login.side_effect = LoginError("Boom")
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "whatever", CONF_USERNAME: "whatever"},
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
assert result["errors"]["base"] == "auth_failed"
|
||||
|
||||
|
||||
async def test_ssdp_not_successful(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow from discovery but no device found."""
|
||||
fritz().login.side_effect = OSError("Boom")
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "whatever", CONF_USERNAME: "whatever"},
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_found"
|
||||
|
||||
|
||||
async def test_ssdp_already_in_progress(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow from discovery twice."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_in_progress"
|
||||
|
||||
|
||||
async def test_ssdp_already_configured(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test starting a flow from discovery when already configured."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result2["type"] == "abort"
|
||||
assert result2["reason"] == "already_configured"
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Tests for the AVM Fritz!Box integration."""
|
||||
from unittest.mock import Mock, call
|
||||
|
||||
from homeassistant.components.fritzbox.const import DOMAIN as FB_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
|
||||
from homeassistant.const import CONF_DEVICES, CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import MOCK_CONFIG, FritzDeviceSwitchMock
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setup of integration."""
|
||||
assert await async_setup_component(hass, FB_DOMAIN, MOCK_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
entries = hass.config_entries.async_entries()
|
||||
assert entries
|
||||
assert entries[0].data[CONF_HOST] == "fake_host"
|
||||
assert entries[0].data[CONF_PASSWORD] == "fake_pass"
|
||||
assert entries[0].data[CONF_USERNAME] == "fake_user"
|
||||
assert fritz.call_count == 1
|
||||
assert fritz.call_args_list == [
|
||||
call(host="fake_host", password="fake_pass", user="fake_user")
|
||||
]
|
||||
|
||||
|
||||
async def test_setup_duplicate_config(hass: HomeAssistantType, fritz: Mock, caplog):
|
||||
"""Test duplicate config of integration."""
|
||||
DUPLICATE = {
|
||||
FB_DOMAIN: {
|
||||
CONF_DEVICES: [
|
||||
MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0],
|
||||
MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0],
|
||||
]
|
||||
}
|
||||
}
|
||||
assert not await async_setup_component(hass, FB_DOMAIN, DUPLICATE)
|
||||
await hass.async_block_till_done()
|
||||
assert not hass.states.async_entity_ids()
|
||||
assert not hass.states.async_all()
|
||||
assert "duplicate host entries found" in caplog.text
|
||||
|
||||
|
||||
async def test_unload(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test unload of integration."""
|
||||
fritz().get_devices.return_value = [FritzDeviceSwitchMock()]
|
||||
entity_id = f"{SWITCH_DOMAIN}.fake_name"
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=FB_DOMAIN,
|
||||
data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0],
|
||||
unique_id=entity_id,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
config_entries = hass.config_entries.async_entries(FB_DOMAIN)
|
||||
assert len(config_entries) == 1
|
||||
assert entry is config_entries[0]
|
||||
|
||||
assert await async_setup_component(hass, FB_DOMAIN, {}) is True
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
assert fritz().logout.call_count == 1
|
||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is None
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Tests for AVM Fritz!Box sensor component."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock
|
||||
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from homeassistant.components.fritzbox.const import (
|
||||
ATTR_STATE_DEVICE_LOCKED,
|
||||
ATTR_STATE_LOCKED,
|
||||
DOMAIN as FB_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import MOCK_CONFIG, FritzDeviceSensorMock
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.fake_name"
|
||||
|
||||
|
||||
async def setup_fritzbox(hass: HomeAssistantType, config: dict):
|
||||
"""Set up mock AVM Fritz!Box."""
|
||||
assert await async_setup_component(hass, FB_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setup of platform."""
|
||||
device = FritzDeviceSensorMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
assert state
|
||||
assert state.state == "1.23"
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake_name"
|
||||
assert state.attributes[ATTR_STATE_DEVICE_LOCKED] == "fake_locked_device"
|
||||
assert state.attributes[ATTR_STATE_LOCKED] == "fake_locked"
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
||||
|
||||
|
||||
async def test_update(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceSensorMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
assert device.update.call_count == 0
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
|
||||
async def test_update_error(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceSensorMock()
|
||||
device.update.side_effect = HTTPError("Boom")
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
assert device.update.call_count == 0
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 2
|
||||
@@ -0,0 +1,121 @@
|
||||
"""Tests for AVM Fritz!Box switch component."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock
|
||||
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from homeassistant.components.fritzbox.const import (
|
||||
ATTR_STATE_DEVICE_LOCKED,
|
||||
ATTR_STATE_LOCKED,
|
||||
ATTR_TEMPERATURE_UNIT,
|
||||
ATTR_TOTAL_CONSUMPTION,
|
||||
ATTR_TOTAL_CONSUMPTION_UNIT,
|
||||
DOMAIN as FB_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.switch import ATTR_CURRENT_POWER_W, DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_TEMPERATURE,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import MOCK_CONFIG, FritzDeviceSwitchMock
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.fake_name"
|
||||
|
||||
|
||||
async def setup_fritzbox(hass: HomeAssistantType, config: dict):
|
||||
"""Set up mock AVM Fritz!Box."""
|
||||
assert await async_setup_component(hass, FB_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test setup of platform."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
assert state
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes[ATTR_CURRENT_POWER_W] == 5.678
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake_name"
|
||||
assert state.attributes[ATTR_STATE_DEVICE_LOCKED] == "fake_locked_device"
|
||||
assert state.attributes[ATTR_STATE_LOCKED] == "fake_locked"
|
||||
assert state.attributes[ATTR_TEMPERATURE] == "135"
|
||||
assert state.attributes[ATTR_TEMPERATURE_UNIT] == TEMP_CELSIUS
|
||||
assert state.attributes[ATTR_TOTAL_CONSUMPTION] == "1.234"
|
||||
assert state.attributes[ATTR_TOTAL_CONSUMPTION_UNIT] == ENERGY_KILO_WATT_HOUR
|
||||
|
||||
|
||||
async def test_turn_on(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test turn device on."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert device.set_switch_state_on.call_count == 1
|
||||
|
||||
|
||||
async def test_turn_off(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test turn device off."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert device.set_switch_state_off.call_count == 1
|
||||
|
||||
|
||||
async def test_update(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
assert device.update.call_count == 0
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
|
||||
async def test_update_error(hass: HomeAssistantType, fritz: Mock):
|
||||
"""Test update with error."""
|
||||
device = FritzDeviceSwitchMock()
|
||||
device.update.side_effect = HTTPError("Boom")
|
||||
fritz().get_devices.return_value = [device]
|
||||
|
||||
await setup_fritzbox(hass, MOCK_CONFIG)
|
||||
assert device.update.call_count == 0
|
||||
assert fritz().login.call_count == 1
|
||||
|
||||
next_update = dt_util.utcnow() + timedelta(seconds=200)
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device.update.call_count == 1
|
||||
assert fritz().login.call_count == 2
|
||||
Reference in new issue
Block a user