diff --git a/homeassistant/components/rest/data.py b/homeassistant/components/rest/data.py index f20b811a887..731d1ffe9c3 100644 --- a/homeassistant/components/rest/data.py +++ b/homeassistant/components/rest/data.py @@ -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] = { diff --git a/tests/components/rest/test_data.py b/tests/components/rest/test_data.py index 3add886a451..4d6bc000fac 100644 --- a/tests/components/rest/test_data.py +++ b/tests/components/rest/test_data.py @@ -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"