2018-03-16 02:50:58 +00:00
|
|
|
"""The tests for the Foobot sensor platform."""
|
|
|
|
|
|
|
|
import asyncio
|
2019-12-09 13:14:40 +00:00
|
|
|
import re
|
2018-03-16 02:50:58 +00:00
|
|
|
|
2019-12-09 13:14:40 +00:00
|
|
|
import pytest
|
2018-03-16 02:50:58 +00:00
|
|
|
|
2019-03-19 06:07:39 +00:00
|
|
|
from homeassistant.components.foobot import sensor as foobot
|
2019-12-09 13:14:40 +00:00
|
|
|
import homeassistant.components.sensor as sensor
|
2020-02-25 01:52:14 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
CONCENTRATION_PARTS_PER_BILLION,
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
2020-04-09 15:41:17 +00:00
|
|
|
HTTP_FORBIDDEN,
|
2020-04-08 21:20:03 +00:00
|
|
|
HTTP_INTERNAL_SERVER_ERROR,
|
2020-02-25 01:52:14 +00:00
|
|
|
TEMP_CELSIUS,
|
2020-02-28 19:46:48 +00:00
|
|
|
UNIT_PERCENTAGE,
|
2020-02-25 01:52:14 +00:00
|
|
|
)
|
2018-03-16 02:50:58 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
|
|
|
from homeassistant.setup import async_setup_component
|
2019-12-09 13:14:40 +00:00
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import MagicMock
|
2018-03-16 02:50:58 +00:00
|
|
|
from tests.common import load_fixture
|
|
|
|
|
|
|
|
VALID_CONFIG = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform": "foobot",
|
|
|
|
"token": "adfdsfasd",
|
|
|
|
"username": "example@example.com",
|
2018-03-16 02:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_setup(hass, aioclient_mock):
|
|
|
|
"""Test the default setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
re.compile("api.foobot.io/v2/owner/.*"),
|
|
|
|
text=load_fixture("foobot_devices.json"),
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
re.compile("api.foobot.io/v2/device/.*"), text=load_fixture("foobot_data.json")
|
|
|
|
)
|
|
|
|
assert await async_setup_component(hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
metrics = {
|
2020-02-25 01:52:14 +00:00
|
|
|
"co2": ["1232.0", CONCENTRATION_PARTS_PER_MILLION],
|
2019-07-31 19:25:30 +00:00
|
|
|
"temperature": ["21.1", TEMP_CELSIUS],
|
2020-02-28 19:46:48 +00:00
|
|
|
"humidity": ["49.5", UNIT_PERCENTAGE],
|
2020-02-25 01:52:14 +00:00
|
|
|
"pm2_5": ["144.8", CONCENTRATION_MICROGRAMS_PER_CUBIC_METER],
|
|
|
|
"voc": ["340.7", CONCENTRATION_PARTS_PER_BILLION],
|
2020-02-28 19:46:48 +00:00
|
|
|
"index": ["138.9", UNIT_PERCENTAGE],
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
2018-03-16 02:50:58 +00:00
|
|
|
|
|
|
|
for name, value in metrics.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.foobot_happybot_%s" % name)
|
2018-03-16 02:50:58 +00:00
|
|
|
assert state.state == value[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == value[1]
|
2018-03-16 02:50:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_timeout_error(hass, aioclient_mock):
|
|
|
|
"""Expected failures caused by a timeout in API response."""
|
2018-08-24 14:37:30 +00:00
|
|
|
fake_async_add_entities = MagicMock()
|
2018-03-16 02:50:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
re.compile("api.foobot.io/v2/owner/.*"), exc=asyncio.TimeoutError()
|
|
|
|
)
|
2018-03-16 02:50:58 +00:00
|
|
|
with pytest.raises(PlatformNotReady):
|
2019-07-31 19:25:30 +00:00
|
|
|
await foobot.async_setup_platform(
|
|
|
|
hass, {"sensor": VALID_CONFIG}, fake_async_add_entities
|
|
|
|
)
|
2018-03-16 02:50:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_permanent_error(hass, aioclient_mock):
|
|
|
|
"""Expected failures caused by permanent errors in API response."""
|
2018-08-24 14:37:30 +00:00
|
|
|
fake_async_add_entities = MagicMock()
|
2018-03-16 02:50:58 +00:00
|
|
|
|
2020-04-09 15:41:17 +00:00
|
|
|
errors = [400, 401, HTTP_FORBIDDEN]
|
2018-03-16 02:50:58 +00:00
|
|
|
for error in errors:
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(re.compile("api.foobot.io/v2/owner/.*"), status=error)
|
|
|
|
result = await foobot.async_setup_platform(
|
|
|
|
hass, {"sensor": VALID_CONFIG}, fake_async_add_entities
|
|
|
|
)
|
2018-03-16 02:50:58 +00:00
|
|
|
assert result is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_temporary_error(hass, aioclient_mock):
|
|
|
|
"""Expected failures caused by temporary errors in API response."""
|
2018-08-24 14:37:30 +00:00
|
|
|
fake_async_add_entities = MagicMock()
|
2018-03-16 02:50:58 +00:00
|
|
|
|
2020-04-08 21:20:03 +00:00
|
|
|
errors = [429, HTTP_INTERNAL_SERVER_ERROR]
|
2018-03-16 02:50:58 +00:00
|
|
|
for error in errors:
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(re.compile("api.foobot.io/v2/owner/.*"), status=error)
|
2018-03-16 02:50:58 +00:00
|
|
|
with pytest.raises(PlatformNotReady):
|
2019-07-31 19:25:30 +00:00
|
|
|
await foobot.async_setup_platform(
|
|
|
|
hass, {"sensor": VALID_CONFIG}, fake_async_add_entities
|
|
|
|
)
|