core/tests/components/rest/test_switch.py

204 lines
7.5 KiB
Python
Raw Normal View History

"""The tests for the REST switch platform."""
import asyncio
import aiohttp
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
import homeassistant.components.rest.switch as rest
from homeassistant.setup import setup_component
from homeassistant.util.async_ import run_coroutine_threadsafe
from homeassistant.helpers.template import Template
from tests.common import get_test_home_assistant, assert_setup_component
class TestRestSwitchSetup:
"""Tests for setting up the REST switch platform."""
def setup_method(self):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self):
"""Stop everything that was started."""
self.hass.stop()
def test_setup_missing_config(self):
"""Test setup with configuration missing required entries."""
assert not run_coroutine_threadsafe(
2019-07-31 19:25:30 +00:00
rest.async_setup_platform(self.hass, {"platform": "rest"}, None),
self.hass.loop,
).result()
def test_setup_missing_schema(self):
"""Test setup with resource missing schema."""
assert not run_coroutine_threadsafe(
2019-07-31 19:25:30 +00:00
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "localhost"}, None
),
self.hass.loop,
).result()
def test_setup_failed_connect(self, aioclient_mock):
"""Test setup when connection error occurs."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get("http://localhost", exc=aiohttp.ClientError)
assert not run_coroutine_threadsafe(
2019-07-31 19:25:30 +00:00
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
),
self.hass.loop,
).result()
def test_setup_timeout(self, aioclient_mock):
"""Test setup when connection timeout occurs."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get("http://localhost", exc=asyncio.TimeoutError())
assert not run_coroutine_threadsafe(
2019-07-31 19:25:30 +00:00
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
),
self.hass.loop,
).result()
def test_setup_minimum(self, aioclient_mock):
"""Test setup with minimum configuration."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get("http://localhost", status=200)
with assert_setup_component(1, "switch"):
assert setup_component(
self.hass,
"switch",
{"switch": {"platform": "rest", "resource": "http://localhost"}},
)
assert aioclient_mock.call_count == 1
def test_setup(self, aioclient_mock):
"""Test setup with valid configuration."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get("http://localhost", status=200)
assert setup_component(
self.hass,
"switch",
{
"switch": {
"platform": "rest",
"name": "foo",
"resource": "http://localhost",
"headers": {"Content-type": "application/json"},
"body_on": "custom on text",
"body_off": "custom off text",
}
},
)
assert aioclient_mock.call_count == 1
2019-07-31 19:25:30 +00:00
assert_setup_component(1, "switch")
class TestRestSwitch:
"""Tests for REST switch platform."""
def setup_method(self):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
2019-07-31 19:25:30 +00:00
self.name = "foo"
self.method = "post"
self.resource = "http://localhost/"
self.headers = {"Content-type": "application/json"}
self.auth = None
2019-07-31 19:25:30 +00:00
self.body_on = Template("on", self.hass)
self.body_off = Template("off", self.hass)
2017-06-13 05:27:25 +00:00
self.switch = rest.RestSwitch(
2019-07-31 19:25:30 +00:00
self.name,
self.resource,
self.method,
self.headers,
self.auth,
self.body_on,
self.body_off,
None,
10,
True,
)
self.switch.hass = self.hass
def teardown_method(self):
"""Stop everything that was started."""
self.hass.stop()
def test_name(self):
"""Test the name."""
assert self.name == self.switch.name
def test_is_on_before_update(self):
"""Test is_on in initial state."""
assert self.switch.is_on is None
def test_turn_on_success(self, aioclient_mock):
"""Test turn_on."""
aioclient_mock.post(self.resource, status=200)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
2019-07-31 19:25:30 +00:00
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on
def test_turn_on_status_not_ok(self, aioclient_mock):
"""Test turn_on when error status returned."""
aioclient_mock.post(self.resource, status=500)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
2019-07-31 19:25:30 +00:00
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on is None
def test_turn_on_timeout(self, aioclient_mock):
"""Test turn_on when timeout occurs."""
aioclient_mock.post(self.resource, status=500)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.switch.is_on is None
def test_turn_off_success(self, aioclient_mock):
"""Test turn_off."""
aioclient_mock.post(self.resource, status=200)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
2019-07-31 19:25:30 +00:00
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
assert not self.switch.is_on
def test_turn_off_status_not_ok(self, aioclient_mock):
"""Test turn_off when error status returned."""
aioclient_mock.post(self.resource, status=500)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
2019-07-31 19:25:30 +00:00
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on is None
def test_turn_off_timeout(self, aioclient_mock):
"""Test turn_off when timeout occurs."""
aioclient_mock.post(self.resource, exc=asyncio.TimeoutError())
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.switch.is_on is None
def test_update_when_on(self, aioclient_mock):
"""Test update when switch is on."""
aioclient_mock.get(self.resource, text=self.body_on.template)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on
def test_update_when_off(self, aioclient_mock):
"""Test update when switch is off."""
aioclient_mock.get(self.resource, text=self.body_off.template)
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert not self.switch.is_on
def test_update_when_unknown(self, aioclient_mock):
"""Test update when unknown status returned."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(self.resource, text="unknown status")
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on is None
def test_update_timeout(self, aioclient_mock):
"""Test update when timeout occurs."""
aioclient_mock.get(self.resource, exc=asyncio.TimeoutError())
2019-07-31 19:25:30 +00:00
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on is None