Modernize rest switch tests (#81306)

* Adjust rest switch tests to use schema validation

* Simplify

* Use async_setup_component in tests

* Rewrite tests

* Add patch

* Remove patch

* Adjust mock
pull/81307/head
epenet 2022-11-07 13:08:05 +01:00 committed by GitHub
parent 9b2a8901b1
commit 934cec9778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 272 additions and 199 deletions

View File

@ -3,90 +3,114 @@ import asyncio
from http import HTTPStatus from http import HTTPStatus
import aiohttp import aiohttp
import pytest
from homeassistant.components.rest import DOMAIN from homeassistant.components.rest import DOMAIN
import homeassistant.components.rest.switch as rest from homeassistant.components.rest.switch import (
from homeassistant.components.switch import SwitchDeviceClass CONF_BODY_OFF,
CONF_BODY_ON,
CONF_STATE_RESOURCE,
)
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SCAN_INTERVAL,
SwitchDeviceClass,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_ENTITY_PICTURE,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
CONF_DEVICE_CLASS, CONF_DEVICE_CLASS,
CONF_HEADERS, CONF_HEADERS,
CONF_ICON,
CONF_METHOD,
CONF_NAME, CONF_NAME,
CONF_PARAMS, CONF_PARAMS,
CONF_PLATFORM, CONF_PLATFORM,
CONF_RESOURCE, CONF_RESOURCE,
CONF_UNIQUE_ID,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
Platform, SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.template import Template from homeassistant.helpers.template_entity import CONF_PICTURE
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from tests.common import assert_setup_component from tests.common import assert_setup_component, async_fire_time_changed
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
NAME = "foo" NAME = "foo"
DEVICE_CLASS = SwitchDeviceClass.SWITCH DEVICE_CLASS = SwitchDeviceClass.SWITCH
METHOD = "post"
RESOURCE = "http://localhost/" RESOURCE = "http://localhost/"
STATE_RESOURCE = RESOURCE STATE_RESOURCE = RESOURCE
PARAMS = None
async def test_setup_missing_config(hass: HomeAssistant) -> None: async def test_setup_missing_config(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup with configuration missing required entries.""" """Test setup with configuration missing required entries."""
assert not await rest.async_setup_platform(hass, {CONF_PLATFORM: DOMAIN}, None) config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN}}
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done()
assert_setup_component(0, SWITCH_DOMAIN)
assert "Invalid config for [switch.rest]: required key not provided" in caplog.text
async def test_setup_missing_schema(hass: HomeAssistant) -> None: async def test_setup_missing_schema(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup with resource missing schema.""" """Test setup with resource missing schema."""
assert not await rest.async_setup_platform( config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: "localhost"}}
hass, assert await async_setup_component(hass, SWITCH_DOMAIN, config)
{CONF_PLATFORM: DOMAIN, CONF_RESOURCE: "localhost"}, await hass.async_block_till_done()
None, assert_setup_component(0, SWITCH_DOMAIN)
) assert "Invalid config for [switch.rest]: invalid url" in caplog.text
async def test_setup_failed_connect( async def test_setup_failed_connect(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test setup when connection error occurs.""" """Test setup when connection error occurs."""
aioclient_mock.get("http://localhost", exc=aiohttp.ClientError) aioclient_mock.get(RESOURCE, exc=aiohttp.ClientError)
assert not await rest.async_setup_platform( config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
hass, assert await async_setup_component(hass, SWITCH_DOMAIN, config)
{CONF_PLATFORM: DOMAIN, CONF_RESOURCE: "http://localhost"}, await hass.async_block_till_done()
None, assert_setup_component(0, SWITCH_DOMAIN)
) assert "No route to resource/endpoint" in caplog.text
async def test_setup_timeout( async def test_setup_timeout(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test setup when connection timeout occurs.""" """Test setup when connection timeout occurs."""
aioclient_mock.get("http://localhost", exc=asyncio.TimeoutError()) aioclient_mock.get(RESOURCE, exc=asyncio.TimeoutError())
assert not await rest.async_setup_platform( config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
hass, assert await async_setup_component(hass, SWITCH_DOMAIN, config)
{CONF_PLATFORM: DOMAIN, CONF_RESOURCE: "http://localhost"}, await hass.async_block_till_done()
None, assert_setup_component(0, SWITCH_DOMAIN)
) assert "No route to resource/endpoint" in caplog.text
async def test_setup_minimum( async def test_setup_minimum(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test setup with minimum configuration.""" """Test setup with minimum configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.OK) aioclient_mock.get(RESOURCE, status=HTTPStatus.OK)
with assert_setup_component(1, Platform.SWITCH): config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
assert await async_setup_component( with assert_setup_component(1, SWITCH_DOMAIN):
hass, assert await async_setup_component(hass, SWITCH_DOMAIN, config)
Platform.SWITCH,
{
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_RESOURCE: "http://localhost",
}
},
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
@ -96,18 +120,15 @@ async def test_setup_query_params(
) -> None: ) -> None:
"""Test setup with query params.""" """Test setup with query params."""
aioclient_mock.get("http://localhost/?search=something", status=HTTPStatus.OK) aioclient_mock.get("http://localhost/?search=something", status=HTTPStatus.OK)
with assert_setup_component(1, Platform.SWITCH): config = {
assert await async_setup_component( SWITCH_DOMAIN: {
hass,
Platform.SWITCH,
{
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN, CONF_PLATFORM: DOMAIN,
CONF_RESOURCE: "http://localhost", CONF_RESOURCE: RESOURCE,
CONF_PARAMS: {"search": "something"}, CONF_PARAMS: {"search": "something"},
} }
}, }
) with assert_setup_component(1, SWITCH_DOMAIN):
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
@ -115,62 +136,53 @@ async def test_setup_query_params(
async def test_setup(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: async def test_setup(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.OK) aioclient_mock.get(RESOURCE, status=HTTPStatus.OK)
assert await async_setup_component( config = {
hass, SWITCH_DOMAIN: {
Platform.SWITCH,
{
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN, CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo", CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost", CONF_RESOURCE: RESOURCE,
CONF_HEADERS: {"Content-type": CONTENT_TYPE_JSON}, CONF_HEADERS: {"Content-type": CONTENT_TYPE_JSON},
rest.CONF_BODY_ON: "custom on text", CONF_BODY_ON: "custom on text",
rest.CONF_BODY_OFF: "custom off text", CONF_BODY_OFF: "custom off text",
} }
}, }
) assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
assert_setup_component(1, Platform.SWITCH) assert_setup_component(1, SWITCH_DOMAIN)
async def test_setup_with_state_resource( async def test_setup_with_state_resource(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.NOT_FOUND) aioclient_mock.get(RESOURCE, status=HTTPStatus.NOT_FOUND)
aioclient_mock.get("http://localhost/state", status=HTTPStatus.OK) aioclient_mock.get("http://localhost/state", status=HTTPStatus.OK)
assert await async_setup_component( config = {
hass, SWITCH_DOMAIN: {
Platform.SWITCH,
{
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN, CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo", CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost", CONF_RESOURCE: RESOURCE,
rest.CONF_STATE_RESOURCE: "http://localhost/state", CONF_STATE_RESOURCE: "http://localhost/state",
CONF_HEADERS: {"Content-type": CONTENT_TYPE_JSON}, CONF_HEADERS: {"Content-type": CONTENT_TYPE_JSON},
rest.CONF_BODY_ON: "custom on text", CONF_BODY_ON: "custom on text",
rest.CONF_BODY_OFF: "custom off text", CONF_BODY_OFF: "custom off text",
} }
}, }
) assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
assert_setup_component(1, Platform.SWITCH) assert_setup_component(1, SWITCH_DOMAIN)
async def test_setup_with_templated_headers_params( async def test_setup_with_templated_headers_params(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.OK) aioclient_mock.get(RESOURCE, status=HTTPStatus.OK)
assert await async_setup_component( config = {
hass, SWITCH_DOMAIN: {
Platform.SWITCH,
{
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN, CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo", CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost", CONF_RESOURCE: "http://localhost",
@ -183,176 +195,237 @@ async def test_setup_with_templated_headers_params(
"end": "{{ 3 + 2 }}", "end": "{{ 3 + 2 }}",
}, },
} }
}, }
) assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
assert aioclient_mock.mock_calls[-1][3].get("Accept") == CONTENT_TYPE_JSON assert aioclient_mock.mock_calls[-1][3].get("Accept") == CONTENT_TYPE_JSON
assert aioclient_mock.mock_calls[-1][3].get("User-Agent") == "Mozilla/5.0" assert aioclient_mock.mock_calls[-1][3].get("User-Agent") == "Mozilla/5.0"
assert aioclient_mock.mock_calls[-1][1].query["start"] == "0" assert aioclient_mock.mock_calls[-1][1].query["start"] == "0"
assert aioclient_mock.mock_calls[-1][1].query["end"] == "5" assert aioclient_mock.mock_calls[-1][1].query["end"] == "5"
assert_setup_component(1, Platform.SWITCH) assert_setup_component(1, SWITCH_DOMAIN)
"""Tests for REST switch platform.""" # Tests for REST switch platform.
def _setup_test_switch(hass: HomeAssistant) -> None: async def _async_setup_test_switch(
body_on = Template("on", hass) hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
body_off = Template("off", hass) ) -> None:
headers = {"Content-type": Template(CONTENT_TYPE_JSON, hass)} aioclient_mock.get(RESOURCE, status=HTTPStatus.OK)
switch = rest.RestSwitch(
hass, headers = {"Content-type": CONTENT_TYPE_JSON}
{ config = {
CONF_NAME: Template(NAME, hass), CONF_PLATFORM: DOMAIN,
CONF_NAME: NAME,
CONF_DEVICE_CLASS: DEVICE_CLASS, CONF_DEVICE_CLASS: DEVICE_CLASS,
CONF_RESOURCE: RESOURCE, CONF_RESOURCE: RESOURCE,
rest.CONF_STATE_RESOURCE: STATE_RESOURCE, CONF_STATE_RESOURCE: STATE_RESOURCE,
rest.CONF_METHOD: METHOD, CONF_HEADERS: headers,
rest.CONF_HEADERS: headers, }
rest.CONF_PARAMS: PARAMS,
rest.CONF_BODY_ON: body_on, assert await async_setup_component(hass, SWITCH_DOMAIN, {SWITCH_DOMAIN: config})
rest.CONF_BODY_OFF: body_off, await hass.async_block_till_done()
rest.CONF_IS_ON_TEMPLATE: None, assert_setup_component(1, SWITCH_DOMAIN)
rest.CONF_TIMEOUT: 10,
rest.CONF_VERIFY_SSL: True, assert hass.states.get("switch.foo").state == STATE_UNKNOWN
}, aioclient_mock.clear_requests()
None,
)
switch.hass = hass
return switch, body_on, body_off
def test_name(hass: HomeAssistant) -> None: async def test_name(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
"""Test the name.""" """Test the name."""
switch, body_on, body_off = _setup_test_switch(hass) await _async_setup_test_switch(hass, aioclient_mock)
assert switch.name == NAME
state = hass.states.get("switch.foo")
assert state.attributes[ATTR_FRIENDLY_NAME] == NAME
def test_device_class(hass: HomeAssistant) -> None: async def test_device_class(
"""Test the name.""" hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
switch, body_on, body_off = _setup_test_switch(hass) ) -> None:
assert switch.device_class == DEVICE_CLASS """Test the device class."""
await _async_setup_test_switch(hass, aioclient_mock)
state = hass.states.get("switch.foo")
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS
def test_is_on_before_update(hass: HomeAssistant) -> None: async def test_is_on_before_update(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test is_on in initial state.""" """Test is_on in initial state."""
switch, body_on, body_off = _setup_test_switch(hass) await _async_setup_test_switch(hass, aioclient_mock)
assert switch.is_on is None
state = hass.states.get("switch.foo")
assert state.state == STATE_UNKNOWN
async def test_turn_on_success( async def test_turn_on_success(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_on.""" """Test turn_on."""
aioclient_mock.post(RESOURCE, status=HTTPStatus.OK) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_on()
assert body_on.template == aioclient_mock.mock_calls[-1][2].decode() aioclient_mock.post(RESOURCE, status=HTTPStatus.OK)
assert switch.is_on aioclient_mock.get(RESOURCE, exc=aiohttp.ClientError)
assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.mock_calls[-2][2].decode() == "ON"
assert hass.states.get("switch.foo").state == STATE_ON
async def test_turn_on_status_not_ok( async def test_turn_on_status_not_ok(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_on when error status returned.""" """Test turn_on when error status returned."""
aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_on()
assert body_on.template == aioclient_mock.mock_calls[-1][2].decode() aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR)
assert switch.is_on is None assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.mock_calls[-1][2].decode() == "ON"
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_turn_on_timeout( async def test_turn_on_timeout(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_on when timeout occurs.""" """Test turn_on when timeout occurs."""
aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_on()
assert switch.is_on is None aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR)
assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_turn_off_success( async def test_turn_off_success(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_off.""" """Test turn_off."""
aioclient_mock.post(RESOURCE, status=HTTPStatus.OK) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_off()
assert body_off.template == aioclient_mock.mock_calls[-1][2].decode() aioclient_mock.post(RESOURCE, status=HTTPStatus.OK)
assert not switch.is_on aioclient_mock.get(RESOURCE, exc=aiohttp.ClientError)
assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.mock_calls[-2][2].decode() == "OFF"
assert hass.states.get("switch.foo").state == STATE_OFF
async def test_turn_off_status_not_ok( async def test_turn_off_status_not_ok(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_off when error status returned.""" """Test turn_off when error status returned."""
aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_off()
assert body_off.template == aioclient_mock.mock_calls[-1][2].decode() aioclient_mock.post(RESOURCE, status=HTTPStatus.INTERNAL_SERVER_ERROR)
assert switch.is_on is None assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.mock_calls[-1][2].decode() == "OFF"
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_turn_off_timeout( async def test_turn_off_timeout(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test turn_off when timeout occurs.""" """Test turn_off when timeout occurs."""
aioclient_mock.post(RESOURCE, exc=asyncio.TimeoutError()) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_turn_on()
assert switch.is_on is None aioclient_mock.post(RESOURCE, exc=asyncio.TimeoutError())
assert await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.foo"},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_update_when_on( async def test_update_when_on(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test update when switch is on.""" """Test update when switch is on."""
switch, body_on, body_off = _setup_test_switch(hass) await _async_setup_test_switch(hass, aioclient_mock)
aioclient_mock.get(RESOURCE, text=body_on.template)
await switch.async_update()
assert switch.is_on aioclient_mock.get(RESOURCE, text="ON")
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_ON
async def test_update_when_off( async def test_update_when_off(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test update when switch is off.""" """Test update when switch is off."""
switch, body_on, body_off = _setup_test_switch(hass) await _async_setup_test_switch(hass, aioclient_mock)
aioclient_mock.get(RESOURCE, text=body_off.template)
await switch.async_update()
assert not switch.is_on aioclient_mock.get(RESOURCE, text="OFF")
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_OFF
async def test_update_when_unknown( async def test_update_when_unknown(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test update when unknown status returned.""" """Test update when unknown status returned."""
aioclient_mock.get(RESOURCE, text="unknown status") await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_update()
assert switch.is_on is None aioclient_mock.get(RESOURCE, text="unknown status")
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_update_timeout( async def test_update_timeout(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test update when timeout occurs.""" """Test update when timeout occurs."""
aioclient_mock.get(RESOURCE, exc=asyncio.TimeoutError()) await _async_setup_test_switch(hass, aioclient_mock)
switch, body_on, body_off = _setup_test_switch(hass)
await switch.async_update()
assert switch.is_on is None aioclient_mock.get(RESOURCE, exc=asyncio.TimeoutError())
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert hass.states.get("switch.foo").state == STATE_UNKNOWN
async def test_entity_config( async def test_entity_config(
@ -360,22 +433,22 @@ async def test_entity_config(
) -> None: ) -> None:
"""Test entity configuration.""" """Test entity configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.OK) aioclient_mock.get(RESOURCE, status=HTTPStatus.OK)
config = { config = {
Platform.SWITCH: { SWITCH_DOMAIN: {
# REST configuration # REST configuration
"platform": "rest", CONF_PLATFORM: "rest",
"method": "POST", CONF_METHOD: "POST",
"resource": "http://localhost", CONF_RESOURCE: "http://localhost",
# Entity configuration # Entity configuration
"icon": "{{'mdi:one_two_three'}}", CONF_ICON: "{{'mdi:one_two_three'}}",
"picture": "{{'blabla.png'}}", CONF_PICTURE: "{{'blabla.png'}}",
"name": "{{'REST' + ' ' + 'Switch'}}", CONF_NAME: "{{'REST' + ' ' + 'Switch'}}",
"unique_id": "very_unique", CONF_UNIQUE_ID: "very_unique",
}, },
} }
assert await async_setup_component(hass, Platform.SWITCH, config) assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
@ -384,7 +457,7 @@ async def test_entity_config(
state = hass.states.get("switch.rest_switch") state = hass.states.get("switch.rest_switch")
assert state.state == "unknown" assert state.state == "unknown"
assert state.attributes == { assert state.attributes == {
"entity_picture": "blabla.png", ATTR_ENTITY_PICTURE: "blabla.png",
"friendly_name": "REST Switch", ATTR_FRIENDLY_NAME: "REST Switch",
"icon": "mdi:one_two_three", ATTR_ICON: "mdi:one_two_three",
} }