Use platform enums in rest tests (#62564)

This commit is contained in:
Robert Hillis 2021-12-22 07:31:55 -05:00 committed by GitHub
parent 496165711d
commit d9788c2447
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 25 deletions

View file

@ -8,7 +8,7 @@ import httpx
import respx
from homeassistant import config as hass_config
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDeviceClass
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
@ -17,6 +17,7 @@ from homeassistant.const import (
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
Platform,
)
from homeassistant.setup import async_setup_component
@ -26,7 +27,7 @@ from tests.common import get_fixture_path
async def test_setup_missing_basic_config(hass):
"""Test setup with configuration missing required entries."""
assert await async_setup_component(
hass, DOMAIN, {"binary_sensor": {"platform": "rest"}}
hass, Platform.BINARY_SENSOR, {"binary_sensor": {"platform": "rest"}}
)
await hass.async_block_till_done()
assert len(hass.states.async_all("binary_sensor")) == 0
@ -36,7 +37,7 @@ async def test_setup_missing_config(hass):
"""Test setup with configuration missing required entries."""
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -58,7 +59,7 @@ async def test_setup_failed_connect(hass, caplog):
)
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -78,7 +79,7 @@ async def test_setup_timeout(hass):
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -97,7 +98,7 @@ async def test_setup_minimum(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -116,7 +117,7 @@ async def test_setup_minimum_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -134,7 +135,7 @@ async def test_setup_duplicate_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",
@ -418,7 +419,7 @@ async def test_setup_query_params(hass):
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
assert await async_setup_component(
hass,
DOMAIN,
Platform.BINARY_SENSOR,
{
"binary_sensor": {
"platform": "rest",

View file

@ -6,7 +6,7 @@ import aiohttp
from homeassistant.components.rest import DOMAIN
import homeassistant.components.rest.switch as rest
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchDeviceClass
from homeassistant.components.switch import SwitchDeviceClass
from homeassistant.const import (
CONF_HEADERS,
CONF_NAME,
@ -14,6 +14,7 @@ from homeassistant.const import (
CONF_PLATFORM,
CONF_RESOURCE,
CONTENT_TYPE_JSON,
Platform,
)
from homeassistant.helpers.template import Template
from homeassistant.setup import async_setup_component
@ -68,12 +69,12 @@ async def test_setup_timeout(hass, aioclient_mock):
async def test_setup_minimum(hass, aioclient_mock):
"""Test setup with minimum configuration."""
aioclient_mock.get("http://localhost", status=HTTPStatus.OK)
with assert_setup_component(1, SWITCH_DOMAIN):
with assert_setup_component(1, Platform.SWITCH):
assert await async_setup_component(
hass,
SWITCH_DOMAIN,
Platform.SWITCH,
{
SWITCH_DOMAIN: {
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_RESOURCE: "http://localhost",
}
@ -86,12 +87,12 @@ async def test_setup_minimum(hass, aioclient_mock):
async def test_setup_query_params(hass, aioclient_mock):
"""Test setup with query params."""
aioclient_mock.get("http://localhost/?search=something", status=HTTPStatus.OK)
with assert_setup_component(1, SWITCH_DOMAIN):
with assert_setup_component(1, Platform.SWITCH):
assert await async_setup_component(
hass,
SWITCH_DOMAIN,
Platform.SWITCH,
{
SWITCH_DOMAIN: {
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_RESOURCE: "http://localhost",
CONF_PARAMS: {"search": "something"},
@ -109,9 +110,9 @@ async def test_setup(hass, aioclient_mock):
aioclient_mock.get("http://localhost", status=HTTPStatus.OK)
assert await async_setup_component(
hass,
SWITCH_DOMAIN,
Platform.SWITCH,
{
SWITCH_DOMAIN: {
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost",
@ -123,7 +124,7 @@ async def test_setup(hass, aioclient_mock):
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 1
assert_setup_component(1, SWITCH_DOMAIN)
assert_setup_component(1, Platform.SWITCH)
async def test_setup_with_state_resource(hass, aioclient_mock):
@ -132,9 +133,9 @@ async def test_setup_with_state_resource(hass, aioclient_mock):
aioclient_mock.get("http://localhost/state", status=HTTPStatus.OK)
assert await async_setup_component(
hass,
SWITCH_DOMAIN,
Platform.SWITCH,
{
SWITCH_DOMAIN: {
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost",
@ -147,7 +148,7 @@ async def test_setup_with_state_resource(hass, aioclient_mock):
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 1
assert_setup_component(1, SWITCH_DOMAIN)
assert_setup_component(1, Platform.SWITCH)
async def test_setup_with_templated_headers_params(hass, aioclient_mock):
@ -155,9 +156,9 @@ async def test_setup_with_templated_headers_params(hass, aioclient_mock):
aioclient_mock.get("http://localhost", status=HTTPStatus.OK)
assert await async_setup_component(
hass,
SWITCH_DOMAIN,
Platform.SWITCH,
{
SWITCH_DOMAIN: {
Platform.SWITCH: {
CONF_PLATFORM: DOMAIN,
CONF_NAME: "foo",
CONF_RESOURCE: "http://localhost",
@ -178,7 +179,7 @@ async def test_setup_with_templated_headers_params(hass, aioclient_mock):
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["end"] == "5"
assert_setup_component(1, SWITCH_DOMAIN)
assert_setup_component(1, Platform.SWITCH)
"""Tests for REST switch platform."""