Preserve httpx boolean behavior in REST integration after aiohttp conversion (#147738)

This commit is contained in:
J. Nick Koston 2025-06-28 17:24:09 -05:00 committed by GitHub
parent bbd1cbf5c9
commit 6d28b99344
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 0 deletions

View File

@ -110,6 +110,12 @@ class RestData:
rendered_headers = template.render_complex(self._headers, parse_result=False)
rendered_params = template.render_complex(self._params)
# Convert boolean values to lowercase strings for compatibility with aiohttp/yarl
if rendered_params:
for key, value in rendered_params.items():
if isinstance(value, bool):
rendered_params[key] = str(value).lower()
_LOGGER.debug("Updating from %s", self._resource)
# Create request kwargs
request_kwargs: dict[str, Any] = {

View File

@ -442,3 +442,52 @@ async def test_rest_data_timeout_error(
"Timeout while fetching data: http://example.com/api" in caplog.text
or "Platform rest not ready yet" in caplog.text
)
async def test_rest_data_boolean_params_converted_to_strings(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that boolean parameters are converted to lowercase strings."""
# Mock the request and capture the actual URL
aioclient_mock.get(
"http://example.com/api",
status=200,
json={"status": "ok"},
headers={"Content-Type": "application/json"},
)
assert await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
"resource": "http://example.com/api",
"method": "GET",
"params": {
"boolTrue": True,
"boolFalse": False,
"stringParam": "test",
"intParam": 123,
},
"sensor": [
{
"name": "test_sensor",
"value_template": "{{ value_json.status }}",
}
],
}
},
)
await hass.async_block_till_done()
# Check that the request was made with boolean values converted to strings
assert len(aioclient_mock.mock_calls) == 1
method, url, data, headers = aioclient_mock.mock_calls[0]
# Check that the URL query parameters have boolean values converted to strings
assert url.query["boolTrue"] == "true"
assert url.query["boolFalse"] == "false"
assert url.query["stringParam"] == "test"
assert url.query["intParam"] == "123"