diff --git a/requirements_test.txt b/requirements_test.txt
index 8ec5a611f1d..e4553a2498b 100644
--- a/requirements_test.txt
+++ b/requirements_test.txt
@@ -24,6 +24,6 @@ pytest-xdist==2.1.0
pytest==6.1.2
requests_mock==1.8.0
responses==0.12.0
-respx==0.14.0
+respx==0.16.2
stdlib-list==0.7.0
tqdm==4.49.0
diff --git a/tests/components/rest/test_binary_sensor.py b/tests/components/rest/test_binary_sensor.py
index 48d13a716ab..2638477ef79 100644
--- a/tests/components/rest/test_binary_sensor.py
+++ b/tests/components/rest/test_binary_sensor.py
@@ -18,7 +18,7 @@ from homeassistant.const import (
)
from homeassistant.setup import async_setup_component
-from tests.async_mock import Mock, patch
+from tests.async_mock import patch
async def test_setup_missing_basic_config(hass):
@@ -50,9 +50,7 @@ async def test_setup_missing_config(hass):
@respx.mock
async def test_setup_failed_connect(hass):
"""Test setup when connection error occurs."""
- respx.get(
- "http://localhost", content=httpx.RequestError(message="any", request=Mock())
- )
+ respx.get("http://localhost").mock(side_effect=httpx.RequestError)
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@@ -71,7 +69,7 @@ async def test_setup_failed_connect(hass):
@respx.mock
async def test_setup_timeout(hass):
"""Test setup when connection timeout occurs."""
- respx.get("http://localhost", content=asyncio.TimeoutError())
+ respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@@ -90,7 +88,7 @@ async def test_setup_timeout(hass):
@respx.mock
async def test_setup_minimum(hass):
"""Test setup with minimum configuration."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@@ -109,7 +107,7 @@ async def test_setup_minimum(hass):
@respx.mock
async def test_setup_minimum_resource_template(hass):
"""Test setup with minimum configuration (resource_template)."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@@ -127,7 +125,7 @@ async def test_setup_minimum_resource_template(hass):
@respx.mock
async def test_setup_duplicate_resource_template(hass):
"""Test setup with duplicate resources."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@@ -146,7 +144,7 @@ async def test_setup_duplicate_resource_template(hass):
@respx.mock
async def test_setup_get(hass):
"""Test setup with valid configuration."""
- respx.get("http://localhost", status_code=200, content="{}")
+ respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@@ -174,7 +172,7 @@ async def test_setup_get(hass):
@respx.mock
async def test_setup_get_digest_auth(hass):
"""Test setup with valid configuration."""
- respx.get("http://localhost", status_code=200, content="{}")
+ respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@@ -202,7 +200,7 @@ async def test_setup_get_digest_auth(hass):
@respx.mock
async def test_setup_post(hass):
"""Test setup with valid configuration."""
- respx.post("http://localhost", status_code=200, content="{}")
+ respx.post("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@@ -230,11 +228,10 @@ async def test_setup_post(hass):
@respx.mock
async def test_setup_get_off(hass):
"""Test setup with valid off configuration."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/json"},
- content='{"dog": false}',
+ json={"dog": False},
)
assert await async_setup_component(
hass,
@@ -261,11 +258,10 @@ async def test_setup_get_off(hass):
@respx.mock
async def test_setup_get_on(hass):
"""Test setup with valid on configuration."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/json"},
- content='{"dog": true}',
+ json={"dog": True},
)
assert await async_setup_component(
hass,
@@ -292,7 +288,7 @@ async def test_setup_get_on(hass):
@respx.mock
async def test_setup_with_exception(hass):
"""Test setup with exception."""
- respx.get("http://localhost", status_code=200, content="{}")
+ respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@@ -318,9 +314,7 @@ async def test_setup_with_exception(hass):
await hass.async_block_till_done()
respx.clear()
- respx.get(
- "http://localhost", content=httpx.RequestError(message="any", request=Mock())
- )
+ respx.get("http://localhost").mock(side_effect=httpx.RequestError)
await hass.services.async_call(
"homeassistant",
"update_entity",
@@ -337,7 +331,7 @@ async def test_setup_with_exception(hass):
async def test_reload(hass):
"""Verify we can reload reset sensors."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
await async_setup_component(
hass,
@@ -380,10 +374,7 @@ async def test_reload(hass):
@respx.mock
async def test_setup_query_params(hass):
"""Test setup with query params."""
- respx.get(
- "http://localhost?search=something",
- status_code=200,
- )
+ respx.get("http://localhost", params={"search": "something"}) % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
diff --git a/tests/components/rest/test_sensor.py b/tests/components/rest/test_sensor.py
index 71bcbedda88..f378a1fc2a1 100644
--- a/tests/components/rest/test_sensor.py
+++ b/tests/components/rest/test_sensor.py
@@ -16,7 +16,7 @@ from homeassistant.const import (
)
from homeassistant.setup import async_setup_component
-from tests.async_mock import Mock, patch
+from tests.async_mock import patch
async def test_setup_missing_config(hass):
@@ -42,9 +42,7 @@ async def test_setup_missing_schema(hass):
@respx.mock
async def test_setup_failed_connect(hass):
"""Test setup when connection error occurs."""
- respx.get(
- "http://localhost", content=httpx.RequestError(message="any", request=Mock())
- )
+ respx.get("http://localhost").mock(side_effect=httpx.RequestError)
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -63,7 +61,7 @@ async def test_setup_failed_connect(hass):
@respx.mock
async def test_setup_timeout(hass):
"""Test setup when connection timeout occurs."""
- respx.get("http://localhost", content=asyncio.TimeoutError())
+ respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -76,7 +74,7 @@ async def test_setup_timeout(hass):
@respx.mock
async def test_setup_minimum(hass):
"""Test setup with minimum configuration."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -95,7 +93,7 @@ async def test_setup_minimum(hass):
@respx.mock
async def test_setup_minimum_resource_template(hass):
"""Test setup with minimum configuration (resource_template)."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -113,7 +111,7 @@ async def test_setup_minimum_resource_template(hass):
@respx.mock
async def test_setup_duplicate_resource_template(hass):
"""Test setup with duplicate resources."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -132,7 +130,7 @@ async def test_setup_duplicate_resource_template(hass):
@respx.mock
async def test_setup_get(hass):
"""Test setup with valid configuration."""
- respx.get("http://localhost", status_code=200, content="{}")
+ respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@@ -161,7 +159,7 @@ async def test_setup_get(hass):
@respx.mock
async def test_setup_get_digest_auth(hass):
"""Test setup with valid configuration."""
- respx.get("http://localhost", status_code=200, content="{}")
+ respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@@ -190,7 +188,7 @@ async def test_setup_get_digest_auth(hass):
@respx.mock
async def test_setup_post(hass):
"""Test setup with valid configuration."""
- respx.post("http://localhost", status_code=200, content="{}")
+ respx.post("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@@ -219,8 +217,7 @@ async def test_setup_post(hass):
@respx.mock
async def test_setup_get_xml(hass):
"""Test setup with valid xml configuration."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="abc",
@@ -252,10 +249,7 @@ async def test_setup_get_xml(hass):
@respx.mock
async def test_setup_query_params(hass):
"""Test setup with query params."""
- respx.get(
- "http://localhost?search=something",
- status_code=200,
- )
+ respx.get("http://localhost", params={"search": "something"}) % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@@ -276,11 +270,9 @@ async def test_setup_query_params(hass):
async def test_update_with_json_attrs(hass):
"""Test attributes get extracted from a JSON result."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
- headers={"content-type": CONTENT_TYPE_JSON},
- content='{ "key": "some_json_value" }',
+ json={"key": "some_json_value"},
)
assert await async_setup_component(
hass,
@@ -311,11 +303,9 @@ async def test_update_with_json_attrs(hass):
async def test_update_with_no_template(hass):
"""Test update when there is no value template."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
- headers={"content-type": CONTENT_TYPE_JSON},
- content='{ "key": "some_json_value" }',
+ json={"key": "some_json_value"},
)
assert await async_setup_component(
hass,
@@ -338,15 +328,14 @@ async def test_update_with_no_template(hass):
assert len(hass.states.async_all()) == 1
state = hass.states.get("sensor.foo")
- assert state.state == '{ "key": "some_json_value" }'
+ assert state.state == '{"key": "some_json_value"}'
@respx.mock
async def test_update_with_json_attrs_no_data(hass, caplog):
"""Test attributes when no JSON result fetched."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content="",
@@ -382,11 +371,9 @@ async def test_update_with_json_attrs_no_data(hass, caplog):
async def test_update_with_json_attrs_not_dict(hass, caplog):
"""Test attributes get extracted from a JSON result."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
- headers={"content-type": CONTENT_TYPE_JSON},
- content='["list", "of", "things"]',
+ json=["list", "of", "things"],
)
assert await async_setup_component(
hass,
@@ -419,8 +406,7 @@ async def test_update_with_json_attrs_not_dict(hass, caplog):
async def test_update_with_json_attrs_bad_JSON(hass, caplog):
"""Test attributes get extracted from a JSON result."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content="This is text rather than JSON data.",
@@ -456,11 +442,17 @@ async def test_update_with_json_attrs_bad_JSON(hass, caplog):
async def test_update_with_json_attrs_with_json_attrs_path(hass):
"""Test attributes get extracted from a JSON result with a template for the attributes."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
- headers={"content-type": CONTENT_TYPE_JSON},
- content='{ "toplevel": {"master_value": "master", "second_level": {"some_json_key": "some_json_value", "some_json_key2": "some_json_value2" } } }',
+ json={
+ "toplevel": {
+ "master_value": "master",
+ "second_level": {
+ "some_json_key": "some_json_value",
+ "some_json_key2": "some_json_value2",
+ },
+ },
+ },
)
assert await async_setup_component(
hass,
@@ -494,8 +486,7 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass):
async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
"""Test attributes get extracted from a JSON result that was converted from XML with a template for the attributes."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="mastersome_json_valuesome_json_value2",
@@ -531,8 +522,7 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(hass):
"""Test attributes get extracted from a JSON result that was converted from XML."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content='01255648alexander000bogus000000000upupupup000x0XF0x0XF 0',
@@ -573,8 +563,7 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
):
"""Test attributes get extracted from a JSON result that was converted from XML with application/xml mime type."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "application/xml"},
content="13",
@@ -610,8 +599,7 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
async def test_update_with_xml_convert_bad_xml(hass, caplog):
"""Test attributes get extracted from a XML result with bad xml."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="",
@@ -646,8 +634,7 @@ async def test_update_with_xml_convert_bad_xml(hass, caplog):
async def test_update_with_failed_get(hass, caplog):
"""Test attributes get extracted from a XML result with bad xml."""
- respx.get(
- "http://localhost",
+ respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="",
@@ -682,7 +669,7 @@ async def test_update_with_failed_get(hass, caplog):
async def test_reload(hass):
"""Verify we can reload reset sensors."""
- respx.get("http://localhost", status_code=200)
+ respx.get("http://localhost") % 200
await async_setup_component(
hass,