Use enums for rest tests (#62197)

Co-authored-by: Franck Nijhof <git@frenck.dev>
pull/62326/head
Robert Hillis 2021-12-19 12:01:33 -05:00 committed by GitHub
parent b869b680fb
commit 1bd904b5b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 36 deletions

View File

@ -8,7 +8,7 @@ import httpx
import respx
from homeassistant import config as hass_config
import homeassistant.components.binary_sensor as binary_sensor
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDeviceClass
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
@ -26,7 +26,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, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "rest"}}
hass, DOMAIN, {"binary_sensor": {"platform": "rest"}}
)
await hass.async_block_till_done()
assert len(hass.states.async_all("binary_sensor")) == 0
@ -36,7 +36,7 @@ async def test_setup_missing_config(hass):
"""Test setup with configuration missing required entries."""
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -58,7 +58,7 @@ async def test_setup_failed_connect(hass, caplog):
)
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -78,7 +78,7 @@ async def test_setup_timeout(hass):
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -97,7 +97,7 @@ async def test_setup_minimum(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -116,7 +116,7 @@ async def test_setup_minimum_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -134,7 +134,7 @@ async def test_setup_duplicate_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",
@ -167,7 +167,7 @@ async def test_setup_get(hass):
"username": "my username",
"password": "my password",
"headers": {"Accept": CONTENT_TYPE_JSON},
"device_class": binary_sensor.DEVICE_CLASS_PLUG,
"device_class": BinarySensorDeviceClass.PLUG,
}
},
)
@ -177,7 +177,7 @@ async def test_setup_get(hass):
state = hass.states.get("binary_sensor.foo")
assert state.state == STATE_OFF
assert state.attributes[ATTR_DEVICE_CLASS] == binary_sensor.DEVICE_CLASS_PLUG
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.PLUG
@respx.mock
@ -418,7 +418,7 @@ async def test_setup_query_params(hass):
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
DOMAIN,
{
"binary_sensor": {
"platform": "rest",

View File

@ -8,15 +8,18 @@ import respx
from homeassistant import config as hass_config
from homeassistant.components.homeassistant import SERVICE_UPDATE_ENTITY
import homeassistant.components.sensor as sensor
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
DOMAIN,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_UNIT_OF_MEASUREMENT,
CONTENT_TYPE_JSON,
DATA_MEGABYTES,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
SERVICE_RELOAD,
STATE_UNKNOWN,
TEMP_CELSIUS,
@ -28,9 +31,7 @@ from tests.common import get_fixture_path
async def test_setup_missing_config(hass):
"""Test setup with configuration missing required entries."""
assert await async_setup_component(
hass, sensor.DOMAIN, {"sensor": {"platform": "rest"}}
)
assert await async_setup_component(hass, DOMAIN, {"sensor": {"platform": "rest"}})
await hass.async_block_till_done()
assert len(hass.states.async_all("sensor")) == 0
@ -39,7 +40,7 @@ async def test_setup_missing_schema(hass):
"""Test setup with resource missing schema."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
)
await hass.async_block_till_done()
@ -54,7 +55,7 @@ async def test_setup_failed_connect(hass, caplog):
)
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"sensor": {
"platform": "rest",
@ -74,7 +75,7 @@ async def test_setup_timeout(hass):
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
)
await hass.async_block_till_done()
@ -87,7 +88,7 @@ async def test_setup_minimum(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"sensor": {
"platform": "rest",
@ -109,7 +110,7 @@ async def test_manual_update(hass):
)
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"sensor": {
"name": "mysensor",
@ -142,7 +143,7 @@ async def test_setup_minimum_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"sensor": {
"platform": "rest",
@ -160,7 +161,7 @@ async def test_setup_duplicate_resource_template(hass):
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"sensor": {
"platform": "rest",
@ -194,8 +195,8 @@ async def test_setup_get(hass):
"username": "my username",
"password": "my password",
"headers": {"Accept": CONTENT_TYPE_JSON},
"device_class": DEVICE_CLASS_TEMPERATURE,
"state_class": sensor.STATE_CLASS_MEASUREMENT,
"device_class": SensorDeviceClass.TEMPERATURE,
"state_class": SensorStateClass.MEASUREMENT,
}
},
)
@ -215,8 +216,8 @@ async def test_setup_get(hass):
state = hass.states.get("sensor.foo")
assert state.state == ""
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TEMPERATURE
assert state.attributes[sensor.ATTR_STATE_CLASS] == sensor.STATE_CLASS_MEASUREMENT
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT
@respx.mock
@ -234,8 +235,8 @@ async def test_setup_timestamp(hass, caplog):
"resource": "http://localhost",
"method": "GET",
"value_template": "{{ value_json.key }}",
"device_class": DEVICE_CLASS_TIMESTAMP,
"state_class": sensor.STATE_CLASS_MEASUREMENT,
"device_class": SensorDeviceClass.TIMESTAMP,
"state_class": SensorStateClass.MEASUREMENT,
}
},
)
@ -246,7 +247,8 @@ async def test_setup_timestamp(hass, caplog):
state = hass.states.get("sensor.rest_sensor")
assert state.state == "2021-11-11T11:39:00+00:00"
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TIMESTAMP
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TIMESTAMP
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT
assert "sensor.rest_sensor rendered invalid timestamp" not in caplog.text
assert "sensor.rest_sensor rendered timestamp without timezone" not in caplog.text
@ -262,7 +264,7 @@ async def test_setup_timestamp(hass, caplog):
)
state = hass.states.get("sensor.rest_sensor")
assert state.state == "unknown"
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TIMESTAMP
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TIMESTAMP
assert "sensor.rest_sensor rendered invalid timestamp" in caplog.text
# Bad response: No timezone
@ -277,7 +279,7 @@ async def test_setup_timestamp(hass, caplog):
)
state = hass.states.get("sensor.rest_sensor")
assert state.state == "unknown"
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TIMESTAMP
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TIMESTAMP
assert "sensor.rest_sensor rendered timestamp without timezone" in caplog.text
@ -411,7 +413,7 @@ async def test_setup_query_params(hass):
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
assert await async_setup_component(
hass,
sensor.DOMAIN,
DOMAIN,
{
"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 DEVICE_CLASS_SWITCH, DOMAIN as SWITCH_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchDeviceClass
from homeassistant.const import (
CONF_HEADERS,
CONF_NAME,
@ -23,7 +23,7 @@ from tests.common import assert_setup_component
"""Tests for setting up the REST switch platform."""
NAME = "foo"
DEVICE_CLASS = DEVICE_CLASS_SWITCH
DEVICE_CLASS = SwitchDeviceClass.SWITCH
METHOD = "post"
RESOURCE = "http://localhost/"
STATE_RESOURCE = RESOURCE