Use domain constants in rest tests (#90765)
This commit is contained in:
parent
3b2127b1dc
commit
f24634e198
3 changed files with 186 additions and 180 deletions
|
@ -9,7 +9,11 @@ import pytest
|
|||
import respx
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DOMAIN as BINARY_SENSOR_DOMAIN,
|
||||
BinarySensorDeviceClass,
|
||||
)
|
||||
from homeassistant.components.rest import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_ENTITY_ID,
|
||||
|
@ -18,7 +22,6 @@ from homeassistant.const import (
|
|||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
@ -30,27 +33,27 @@ from tests.common import get_fixture_path
|
|||
async def test_setup_missing_basic_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup with configuration missing required entries."""
|
||||
assert await async_setup_component(
|
||||
hass, Platform.BINARY_SENSOR, {"binary_sensor": {"platform": "rest"}}
|
||||
hass, BINARY_SENSOR_DOMAIN, {BINARY_SENSOR_DOMAIN: {"platform": DOMAIN}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_setup_missing_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup with configuration missing required entries."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -64,17 +67,17 @@ async def test_setup_failed_connect(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 0
|
||||
assert "server offline" in caplog.text
|
||||
|
||||
|
||||
|
@ -84,17 +87,17 @@ async def test_setup_timeout(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -103,17 +106,17 @@ async def test_setup_minimum(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -122,16 +125,16 @@ async def test_setup_minimum_resource_template(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource_template": "{% set url = 'http://localhost' %}{{ url }}",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -140,17 +143,17 @@ async def test_setup_duplicate_resource_template(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"resource_template": "http://localhost",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 0
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -159,10 +162,10 @@ async def test_setup_get(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -179,7 +182,7 @@ async def test_setup_get(hass: HomeAssistant) -> None:
|
|||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("binary_sensor.foo")
|
||||
assert state.state == STATE_OFF
|
||||
|
@ -195,7 +198,7 @@ async def test_setup_get_template_headers_params(hass: HomeAssistant) -> None:
|
|||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -227,10 +230,10 @@ async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -246,7 +249,7 @@ async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
|
|||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -255,10 +258,10 @@ async def test_setup_post(hass: HomeAssistant) -> None:
|
|||
respx.post("http://localhost").respond(status_code=HTTPStatus.OK, json={})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "POST",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -274,7 +277,7 @@ async def test_setup_post(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -287,10 +290,10 @@ async def test_setup_get_off(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.dog }}",
|
||||
|
@ -301,7 +304,7 @@ async def test_setup_get_off(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("binary_sensor.foo")
|
||||
assert state.state == STATE_OFF
|
||||
|
@ -317,10 +320,10 @@ async def test_setup_get_on(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.dog }}",
|
||||
|
@ -331,7 +334,7 @@ async def test_setup_get_on(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("binary_sensor.foo")
|
||||
assert state.state == STATE_ON
|
||||
|
@ -343,10 +346,10 @@ async def test_setup_with_exception(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.dog }}",
|
||||
|
@ -357,7 +360,7 @@ async def test_setup_with_exception(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("binary_sensor.foo")
|
||||
assert state.state == STATE_OFF
|
||||
|
@ -387,10 +390,10 @@ async def test_reload(hass: HomeAssistant) -> None:
|
|||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"binary_sensor",
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"method": "GET",
|
||||
"name": "mockrest",
|
||||
"resource": "http://localhost",
|
||||
|
@ -401,14 +404,14 @@ async def test_reload(hass: HomeAssistant) -> None:
|
|||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
assert hass.states.get("binary_sensor.mockrest")
|
||||
|
||||
yaml_path = get_fixture_path("configuration.yaml", "rest")
|
||||
yaml_path = get_fixture_path("configuration.yaml", DOMAIN)
|
||||
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
||||
await hass.services.async_call(
|
||||
"rest",
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
{},
|
||||
blocking=True,
|
||||
|
@ -425,10 +428,10 @@ async def test_setup_query_params(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
{
|
||||
"binary_sensor": {
|
||||
"platform": "rest",
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"params": {"search": "something"},
|
||||
|
@ -436,7 +439,7 @@ async def test_setup_query_params(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("binary_sensor")) == 1
|
||||
assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -444,9 +447,9 @@ async def test_entity_config(hass: HomeAssistant) -> None:
|
|||
"""Test entity configuration."""
|
||||
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
BINARY_SENSOR_DOMAIN: {
|
||||
# REST configuration
|
||||
"platform": "rest",
|
||||
"platform": DOMAIN,
|
||||
"method": "GET",
|
||||
"resource": "http://localhost",
|
||||
# Entity configuration
|
||||
|
@ -458,7 +461,7 @@ async def test_entity_config(hass: HomeAssistant) -> None:
|
|||
}
|
||||
|
||||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
assert await async_setup_component(hass, BINARY_SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
|
|
|
@ -9,9 +9,10 @@ import respx
|
|||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.homeassistant import SERVICE_UPDATE_ENTITY
|
||||
from homeassistant.components.rest import DOMAIN
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_STATE_CLASS,
|
||||
DOMAIN,
|
||||
DOMAIN as SENSOR_DOMAIN,
|
||||
SensorDeviceClass,
|
||||
SensorStateClass,
|
||||
)
|
||||
|
@ -34,20 +35,22 @@ from tests.common import get_fixture_path
|
|||
|
||||
async def test_setup_missing_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup with configuration missing required entries."""
|
||||
assert await async_setup_component(hass, DOMAIN, {"sensor": {"platform": "rest"}})
|
||||
assert await async_setup_component(
|
||||
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: {"platform": DOMAIN}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 0
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_setup_missing_schema(hass: HomeAssistant) -> None:
|
||||
"""Test setup with resource missing schema."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
||||
SENSOR_DOMAIN,
|
||||
{SENSOR_DOMAIN: {"platform": DOMAIN, "resource": "localhost", "method": "GET"}},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 0
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -60,17 +63,17 @@ async def test_setup_failed_connect(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 0
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 0
|
||||
assert "server offline" in caplog.text
|
||||
|
||||
|
||||
|
@ -80,11 +83,11 @@ async def test_setup_timeout(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{"sensor": {"platform": "rest", "resource": "localhost", "method": "GET"}},
|
||||
SENSOR_DOMAIN,
|
||||
{SENSOR_DOMAIN: {"platform": DOMAIN, "resource": "localhost", "method": "GET"}},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 0
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -93,17 +96,17 @@ async def test_setup_minimum(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -115,19 +118,19 @@ async def test_setup_encoding(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
SENSOR_DOMAIN: {
|
||||
"name": "mysensor",
|
||||
"encoding": "iso-8859-1",
|
||||
"platform": "rest",
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
assert hass.states.get("sensor.mysensor").state == "tack själv"
|
||||
|
||||
|
||||
|
@ -140,19 +143,19 @@ async def test_manual_update(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
SENSOR_DOMAIN: {
|
||||
"name": "mysensor",
|
||||
"value_template": "{{ value_json.data }}",
|
||||
"platform": "rest",
|
||||
"platform": DOMAIN,
|
||||
"resource_template": "{% set url = 'http://localhost' %}{{ url }}",
|
||||
"method": "GET",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
assert hass.states.get("sensor.mysensor").state == "first"
|
||||
|
||||
respx.get("http://localhost").respond(
|
||||
|
@ -173,16 +176,16 @@ async def test_setup_minimum_resource_template(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource_template": "{% set url = 'http://localhost' %}{{ url }}",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -191,17 +194,17 @@ async def test_setup_duplicate_resource_template(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost") % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"resource_template": "http://localhost",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 0
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 0
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -212,10 +215,10 @@ async def test_setup_get(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -235,7 +238,7 @@ async def test_setup_get(hass: HomeAssistant) -> None:
|
|||
await async_setup_component(hass, "homeassistant", {})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
assert hass.states.get("sensor.foo").state == "123"
|
||||
await hass.services.async_call(
|
||||
|
@ -262,10 +265,10 @@ async def test_setup_timestamp(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -276,7 +279,7 @@ async def test_setup_timestamp(
|
|||
await async_setup_component(hass, "homeassistant", {})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.rest_sensor")
|
||||
assert state.state == "2021-11-11T11:39:00+00:00"
|
||||
|
@ -321,10 +324,10 @@ async def test_setup_get_templated_headers_params(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost").respond(status_code=200, json={})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -358,10 +361,10 @@ async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -378,7 +381,7 @@ async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
|
|||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -389,10 +392,10 @@ async def test_setup_post(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "POST",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -409,7 +412,7 @@ async def test_setup_post(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -422,10 +425,10 @@ async def test_setup_get_xml(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.dog }}",
|
||||
|
@ -437,7 +440,7 @@ async def test_setup_get_xml(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == "123"
|
||||
|
@ -450,10 +453,10 @@ async def test_setup_query_params(hass: HomeAssistant) -> None:
|
|||
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"params": {"search": "something"},
|
||||
|
@ -461,7 +464,7 @@ async def test_setup_query_params(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
|
||||
@respx.mock
|
||||
|
@ -474,10 +477,10 @@ async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -490,7 +493,7 @@ async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == "123"
|
||||
|
@ -507,10 +510,10 @@ async def test_update_with_no_template(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"json_attributes": ["key"],
|
||||
|
@ -522,7 +525,7 @@ async def test_update_with_no_template(hass: HomeAssistant) -> None:
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == '{"key": "some_json_value"}'
|
||||
|
@ -541,10 +544,10 @@ async def test_update_with_json_attrs_no_data(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -558,7 +561,7 @@ async def test_update_with_json_attrs_no_data(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
@ -578,10 +581,10 @@ async def test_update_with_json_attrs_not_dict(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -594,7 +597,7 @@ async def test_update_with_json_attrs_not_dict(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == ""
|
||||
|
@ -615,10 +618,10 @@ async def test_update_with_json_attrs_bad_JSON(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.key }}",
|
||||
|
@ -632,7 +635,7 @@ async def test_update_with_json_attrs_bad_JSON(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
state = hass.states.get("sensor.foo")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
@ -658,10 +661,10 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass: HomeAssistant)
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.toplevel.master_value }}",
|
||||
|
@ -676,7 +679,7 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass: HomeAssistant)
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == "123"
|
||||
|
@ -697,10 +700,10 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.toplevel.master_value }}",
|
||||
|
@ -714,7 +717,7 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == "123"
|
||||
|
@ -735,10 +738,10 @@ async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.response.bss.wlan }}",
|
||||
|
@ -752,7 +755,7 @@ async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == "123"
|
||||
|
@ -776,10 +779,10 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.main.dog }}",
|
||||
|
@ -793,7 +796,7 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == "1"
|
||||
|
@ -814,10 +817,10 @@ async def test_update_with_xml_convert_bad_xml(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.toplevel.master_value }}",
|
||||
|
@ -830,7 +833,7 @@ async def test_update_with_xml_convert_bad_xml(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
@ -851,10 +854,10 @@ async def test_update_with_failed_get(
|
|||
)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"resource": "http://localhost",
|
||||
"method": "GET",
|
||||
"value_template": "{{ value_json.toplevel.master_value }}",
|
||||
|
@ -867,7 +870,7 @@ async def test_update_with_failed_get(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
state = hass.states.get("sensor.foo")
|
||||
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
@ -883,10 +886,10 @@ async def test_reload(hass: HomeAssistant) -> None:
|
|||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
SENSOR_DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "rest",
|
||||
SENSOR_DOMAIN: {
|
||||
"platform": DOMAIN,
|
||||
"method": "GET",
|
||||
"name": "mockrest",
|
||||
"resource": "http://localhost",
|
||||
|
@ -897,14 +900,14 @@ async def test_reload(hass: HomeAssistant) -> None:
|
|||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all("sensor")) == 1
|
||||
assert len(hass.states.async_all(SENSOR_DOMAIN)) == 1
|
||||
|
||||
assert hass.states.get("sensor.mockrest")
|
||||
|
||||
yaml_path = get_fixture_path("configuration.yaml", "rest")
|
||||
yaml_path = get_fixture_path("configuration.yaml", DOMAIN)
|
||||
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
||||
await hass.services.async_call(
|
||||
"rest",
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
{},
|
||||
blocking=True,
|
||||
|
@ -920,9 +923,9 @@ async def test_entity_config(hass: HomeAssistant) -> None:
|
|||
"""Test entity configuration."""
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
SENSOR_DOMAIN: {
|
||||
# REST configuration
|
||||
"platform": "rest",
|
||||
"platform": DOMAIN,
|
||||
"method": "GET",
|
||||
"resource": "http://localhost",
|
||||
# Entity configuration
|
||||
|
@ -937,7 +940,7 @@ async def test_entity_config(hass: HomeAssistant) -> None:
|
|||
}
|
||||
|
||||
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, text="123")
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
|
|
|
@ -437,7 +437,7 @@ async def test_entity_config(
|
|||
config = {
|
||||
SWITCH_DOMAIN: {
|
||||
# REST configuration
|
||||
CONF_PLATFORM: "rest",
|
||||
CONF_PLATFORM: DOMAIN,
|
||||
CONF_METHOD: "POST",
|
||||
CONF_RESOURCE: "http://localhost",
|
||||
# Entity configuration
|
||||
|
|
Loading…
Add table
Reference in a new issue