2020-04-20 13:00:07 +00:00
|
|
|
"""Tests for AVM Fritz!Box sensor component."""
|
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import Mock
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
|
|
|
|
from homeassistant.components.fritzbox.const import (
|
|
|
|
ATTR_STATE_DEVICE_LOCKED,
|
|
|
|
ATTR_STATE_LOCKED,
|
|
|
|
DOMAIN as FB_DOMAIN,
|
|
|
|
)
|
2021-12-21 18:17:23 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, DOMAIN, SensorStateClass
|
2020-04-20 13:00:07 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
2021-04-25 19:49:08 +00:00
|
|
|
CONF_DEVICES,
|
2021-04-18 22:30:58 +00:00
|
|
|
PERCENTAGE,
|
2020-04-20 13:00:07 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2021-04-22 01:53:06 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-04-20 13:00:07 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2021-07-22 16:19:39 +00:00
|
|
|
from . import FritzDeviceSensorMock, setup_config_entry
|
|
|
|
from .const import CONF_FAKE_NAME, MOCK_CONFIG
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
|
|
|
|
2021-07-22 16:19:39 +00:00
|
|
|
ENTITY_ID = f"{DOMAIN}.{CONF_FAKE_NAME}"
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
|
2021-04-22 01:53:06 +00:00
|
|
|
async def test_setup(hass: HomeAssistant, fritz: Mock):
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Test setup of platform."""
|
|
|
|
device = FritzDeviceSensorMock()
|
2021-04-25 19:49:08 +00:00
|
|
|
assert await setup_config_entry(
|
|
|
|
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
|
|
|
)
|
2021-07-22 16:19:39 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-07-22 16:19:39 +00:00
|
|
|
state = hass.states.get(f"{ENTITY_ID}_temperature")
|
2020-04-20 13:00:07 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "1.23"
|
2021-07-22 16:19:39 +00:00
|
|
|
assert state.attributes[ATTR_FRIENDLY_NAME] == f"{CONF_FAKE_NAME} Temperature"
|
2020-04-20 13:00:07 +00:00
|
|
|
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
|
2021-12-21 18:17:23 +00:00
|
|
|
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-09-30 09:56:38 +00:00
|
|
|
state = hass.states.get(f"{ENTITY_ID}_humidity")
|
|
|
|
assert state
|
|
|
|
assert state.state == "42"
|
|
|
|
assert state.attributes[ATTR_FRIENDLY_NAME] == f"{CONF_FAKE_NAME} Humidity"
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
|
2021-12-21 18:17:23 +00:00
|
|
|
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT
|
2021-09-30 09:56:38 +00:00
|
|
|
|
2021-04-18 22:30:58 +00:00
|
|
|
state = hass.states.get(f"{ENTITY_ID}_battery")
|
|
|
|
assert state
|
|
|
|
assert state.state == "23"
|
2021-07-22 16:19:39 +00:00
|
|
|
assert state.attributes[ATTR_FRIENDLY_NAME] == f"{CONF_FAKE_NAME} Battery"
|
2021-04-18 22:30:58 +00:00
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
|
2021-07-22 16:19:39 +00:00
|
|
|
assert ATTR_STATE_CLASS not in state.attributes
|
2021-04-18 22:30:58 +00:00
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-04-22 01:53:06 +00:00
|
|
|
async def test_update(hass: HomeAssistant, fritz: Mock):
|
2021-04-25 00:40:12 +00:00
|
|
|
"""Test update without error."""
|
2020-04-20 13:00:07 +00:00
|
|
|
device = FritzDeviceSensorMock()
|
2021-04-25 19:49:08 +00:00
|
|
|
assert await setup_config_entry(
|
|
|
|
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
|
|
|
)
|
2021-10-14 20:53:05 +00:00
|
|
|
assert fritz().update_devices.call_count == 1
|
2020-04-20 13:00:07 +00:00
|
|
|
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()
|
|
|
|
|
2021-10-14 20:53:05 +00:00
|
|
|
assert fritz().update_devices.call_count == 2
|
2020-04-20 13:00:07 +00:00
|
|
|
assert fritz().login.call_count == 1
|
|
|
|
|
|
|
|
|
2021-04-22 01:53:06 +00:00
|
|
|
async def test_update_error(hass: HomeAssistant, fritz: Mock):
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Test update with error."""
|
|
|
|
device = FritzDeviceSensorMock()
|
2021-10-14 20:53:05 +00:00
|
|
|
fritz().update_devices.side_effect = HTTPError("Boom")
|
2021-04-25 19:49:08 +00:00
|
|
|
assert not await setup_config_entry(
|
|
|
|
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
|
|
|
|
)
|
2021-10-14 20:53:05 +00:00
|
|
|
assert fritz().update_devices.call_count == 1
|
2020-04-20 13:00:07 +00:00
|
|
|
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()
|
|
|
|
|
2021-10-14 20:53:05 +00:00
|
|
|
assert fritz().update_devices.call_count == 2
|
2020-04-20 13:00:07 +00:00
|
|
|
assert fritz().login.call_count == 2
|