core/tests/components/foobot/test_sensor.py

96 lines
3.1 KiB
Python
Raw Normal View History

"""The tests for the Foobot sensor platform."""
import asyncio
import re
import pytest
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.foobot import sensor as foobot
import homeassistant.components.sensor as sensor
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
HTTP_FORBIDDEN,
HTTP_INTERNAL_SERVER_ERROR,
TEMP_CELSIUS,
UNIT_PERCENTAGE,
)
from homeassistant.exceptions import PlatformNotReady
from homeassistant.setup import async_setup_component
from tests.async_mock import MagicMock
from tests.common import load_fixture
VALID_CONFIG = {
2019-07-31 19:25:30 +00:00
"platform": "foobot",
"token": "adfdsfasd",
"username": "example@example.com",
}
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})
await hass.async_block_till_done()
2019-07-31 19:25:30 +00:00
metrics = {
"co2": ["1232.0", CONCENTRATION_PARTS_PER_MILLION],
2019-07-31 19:25:30 +00:00
"temperature": ["21.1", TEMP_CELSIUS],
"humidity": ["49.5", UNIT_PERCENTAGE],
"pm2_5": ["144.8", CONCENTRATION_MICROGRAMS_PER_CUBIC_METER],
"voc": ["340.7", CONCENTRATION_PARTS_PER_BILLION],
"index": ["138.9", UNIT_PERCENTAGE],
2019-07-31 19:25:30 +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)
assert state.state == value[0]
2019-07-31 19:25:30 +00:00
assert state.attributes.get("unit_of_measurement") == value[1]
async def test_setup_timeout_error(hass, aioclient_mock):
"""Expected failures caused by a timeout in API response."""
fake_async_add_entities = MagicMock()
2019-07-31 19:25:30 +00:00
aioclient_mock.get(
re.compile("api.foobot.io/v2/owner/.*"), exc=asyncio.TimeoutError()
)
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
)
async def test_setup_permanent_error(hass, aioclient_mock):
"""Expected failures caused by permanent errors in API response."""
fake_async_add_entities = MagicMock()
errors = [400, 401, HTTP_FORBIDDEN]
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
)
assert result is None
async def test_setup_temporary_error(hass, aioclient_mock):
"""Expected failures caused by temporary errors in API response."""
fake_async_add_entities = MagicMock()
errors = [429, HTTP_INTERNAL_SERVER_ERROR]
for error in errors:
2019-07-31 19:25:30 +00:00
aioclient_mock.get(re.compile("api.foobot.io/v2/owner/.*"), status=error)
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
)