Use enums for rest tests (#62197)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
b869b680fb
commit
1bd904b5b5
3 changed files with 38 additions and 36 deletions
|
@ -8,7 +8,7 @@ import httpx
|
||||||
import respx
|
import respx
|
||||||
|
|
||||||
from homeassistant import config as hass_config
|
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 (
|
from homeassistant.const import (
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
|
@ -26,7 +26,7 @@ from tests.common import get_fixture_path
|
||||||
async def test_setup_missing_basic_config(hass):
|
async def test_setup_missing_basic_config(hass):
|
||||||
"""Test setup with configuration missing required entries."""
|
"""Test setup with configuration missing required entries."""
|
||||||
assert await async_setup_component(
|
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()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
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."""
|
"""Test setup with configuration missing required entries."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -58,7 +58,7 @@ async def test_setup_failed_connect(hass, caplog):
|
||||||
)
|
)
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -78,7 +78,7 @@ async def test_setup_timeout(hass):
|
||||||
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -97,7 +97,7 @@ async def test_setup_minimum(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -116,7 +116,7 @@ async def test_setup_minimum_resource_template(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -134,7 +134,7 @@ async def test_setup_duplicate_resource_template(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -167,7 +167,7 @@ async def test_setup_get(hass):
|
||||||
"username": "my username",
|
"username": "my username",
|
||||||
"password": "my password",
|
"password": "my password",
|
||||||
"headers": {"Accept": CONTENT_TYPE_JSON},
|
"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")
|
state = hass.states.get("binary_sensor.foo")
|
||||||
assert state.state == STATE_OFF
|
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
|
@respx.mock
|
||||||
|
@ -418,7 +418,7 @@ async def test_setup_query_params(hass):
|
||||||
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
|
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
|
|
@ -8,15 +8,18 @@ import respx
|
||||||
|
|
||||||
from homeassistant import config as hass_config
|
from homeassistant import config as hass_config
|
||||||
from homeassistant.components.homeassistant import SERVICE_UPDATE_ENTITY
|
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 (
|
from homeassistant.const import (
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
CONTENT_TYPE_JSON,
|
CONTENT_TYPE_JSON,
|
||||||
DATA_MEGABYTES,
|
DATA_MEGABYTES,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
|
@ -28,9 +31,7 @@ from tests.common import get_fixture_path
|
||||||
|
|
||||||
async def test_setup_missing_config(hass):
|
async def test_setup_missing_config(hass):
|
||||||
"""Test setup with configuration missing required entries."""
|
"""Test setup with configuration missing required entries."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(hass, DOMAIN, {"sensor": {"platform": "rest"}})
|
||||||
hass, sensor.DOMAIN, {"sensor": {"platform": "rest"}}
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_all("sensor")) == 0
|
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."""
|
"""Test setup with resource missing schema."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -54,7 +55,7 @@ async def test_setup_failed_connect(hass, caplog):
|
||||||
)
|
)
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -74,7 +75,7 @@ async def test_setup_timeout(hass):
|
||||||
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -87,7 +88,7 @@ async def test_setup_minimum(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -109,7 +110,7 @@ async def test_manual_update(hass):
|
||||||
)
|
)
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"name": "mysensor",
|
"name": "mysensor",
|
||||||
|
@ -142,7 +143,7 @@ async def test_setup_minimum_resource_template(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -160,7 +161,7 @@ async def test_setup_duplicate_resource_template(hass):
|
||||||
respx.get("http://localhost") % HTTPStatus.OK
|
respx.get("http://localhost") % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
@ -194,8 +195,8 @@ async def test_setup_get(hass):
|
||||||
"username": "my username",
|
"username": "my username",
|
||||||
"password": "my password",
|
"password": "my password",
|
||||||
"headers": {"Accept": CONTENT_TYPE_JSON},
|
"headers": {"Accept": CONTENT_TYPE_JSON},
|
||||||
"device_class": DEVICE_CLASS_TEMPERATURE,
|
"device_class": SensorDeviceClass.TEMPERATURE,
|
||||||
"state_class": sensor.STATE_CLASS_MEASUREMENT,
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -215,8 +216,8 @@ async def test_setup_get(hass):
|
||||||
state = hass.states.get("sensor.foo")
|
state = hass.states.get("sensor.foo")
|
||||||
assert state.state == ""
|
assert state.state == ""
|
||||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
||||||
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TEMPERATURE
|
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
|
||||||
assert state.attributes[sensor.ATTR_STATE_CLASS] == sensor.STATE_CLASS_MEASUREMENT
|
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
|
@ -234,8 +235,8 @@ async def test_setup_timestamp(hass, caplog):
|
||||||
"resource": "http://localhost",
|
"resource": "http://localhost",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"value_template": "{{ value_json.key }}",
|
"value_template": "{{ value_json.key }}",
|
||||||
"device_class": DEVICE_CLASS_TIMESTAMP,
|
"device_class": SensorDeviceClass.TIMESTAMP,
|
||||||
"state_class": sensor.STATE_CLASS_MEASUREMENT,
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -246,7 +247,8 @@ async def test_setup_timestamp(hass, caplog):
|
||||||
|
|
||||||
state = hass.states.get("sensor.rest_sensor")
|
state = hass.states.get("sensor.rest_sensor")
|
||||||
assert state.state == "2021-11-11T11:39:00+00:00"
|
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 invalid timestamp" not in caplog.text
|
||||||
assert "sensor.rest_sensor rendered timestamp without timezone" 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")
|
state = hass.states.get("sensor.rest_sensor")
|
||||||
assert state.state == "unknown"
|
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
|
assert "sensor.rest_sensor rendered invalid timestamp" in caplog.text
|
||||||
|
|
||||||
# Bad response: No timezone
|
# Bad response: No timezone
|
||||||
|
@ -277,7 +279,7 @@ async def test_setup_timestamp(hass, caplog):
|
||||||
)
|
)
|
||||||
state = hass.states.get("sensor.rest_sensor")
|
state = hass.states.get("sensor.rest_sensor")
|
||||||
assert state.state == "unknown"
|
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
|
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
|
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
DOMAIN,
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "rest",
|
"platform": "rest",
|
||||||
|
|
|
@ -6,7 +6,7 @@ import aiohttp
|
||||||
|
|
||||||
from homeassistant.components.rest import DOMAIN
|
from homeassistant.components.rest import DOMAIN
|
||||||
import homeassistant.components.rest.switch as rest
|
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 (
|
from homeassistant.const import (
|
||||||
CONF_HEADERS,
|
CONF_HEADERS,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
@ -23,7 +23,7 @@ from tests.common import assert_setup_component
|
||||||
"""Tests for setting up the REST switch platform."""
|
"""Tests for setting up the REST switch platform."""
|
||||||
|
|
||||||
NAME = "foo"
|
NAME = "foo"
|
||||||
DEVICE_CLASS = DEVICE_CLASS_SWITCH
|
DEVICE_CLASS = SwitchDeviceClass.SWITCH
|
||||||
METHOD = "post"
|
METHOD = "post"
|
||||||
RESOURCE = "http://localhost/"
|
RESOURCE = "http://localhost/"
|
||||||
STATE_RESOURCE = RESOURCE
|
STATE_RESOURCE = RESOURCE
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue