@@ -162,6 +162,94 @@ async def test_setup_encoding(
162162 assert hass .states .get ("sensor.mysensor" ).state == "tack själv"
163163
164164
165+ async def test_setup_auto_encoding_from_content_type (
166+ hass : HomeAssistant , aioclient_mock : AiohttpClientMocker
167+ ) -> None :
168+ """Test setup with encoding auto-detected from Content-Type header."""
169+ # Test with ISO-8859-1 charset in Content-Type header
170+ aioclient_mock .get (
171+ "http://localhost" ,
172+ status = HTTPStatus .OK ,
173+ content = "Björk Guðmundsdóttir" .encode ("iso-8859-1" ),
174+ headers = {"Content-Type" : "text/plain; charset=iso-8859-1" },
175+ )
176+ assert await async_setup_component (
177+ hass ,
178+ SENSOR_DOMAIN ,
179+ {
180+ SENSOR_DOMAIN : {
181+ "name" : "mysensor" ,
182+ # encoding defaults to UTF-8, but should be ignored when charset present
183+ "platform" : DOMAIN ,
184+ "resource" : "http://localhost" ,
185+ "method" : "GET" ,
186+ }
187+ },
188+ )
189+ await hass .async_block_till_done ()
190+ assert len (hass .states .async_all (SENSOR_DOMAIN )) == 1
191+ assert hass .states .get ("sensor.mysensor" ).state == "Björk Guðmundsdóttir"
192+
193+
194+ async def test_setup_encoding_fallback_no_charset (
195+ hass : HomeAssistant , aioclient_mock : AiohttpClientMocker
196+ ) -> None :
197+ """Test that configured encoding is used when no charset in Content-Type."""
198+ # No charset in Content-Type header
199+ aioclient_mock .get (
200+ "http://localhost" ,
201+ status = HTTPStatus .OK ,
202+ content = "Björk Guðmundsdóttir" .encode ("iso-8859-1" ),
203+ headers = {"Content-Type" : "text/plain" }, # No charset!
204+ )
205+ assert await async_setup_component (
206+ hass ,
207+ SENSOR_DOMAIN ,
208+ {
209+ SENSOR_DOMAIN : {
210+ "name" : "mysensor" ,
211+ "encoding" : "iso-8859-1" , # This will be used as fallback
212+ "platform" : DOMAIN ,
213+ "resource" : "http://localhost" ,
214+ "method" : "GET" ,
215+ }
216+ },
217+ )
218+ await hass .async_block_till_done ()
219+ assert len (hass .states .async_all (SENSOR_DOMAIN )) == 1
220+ assert hass .states .get ("sensor.mysensor" ).state == "Björk Guðmundsdóttir"
221+
222+
223+ async def test_setup_charset_overrides_encoding_config (
224+ hass : HomeAssistant , aioclient_mock : AiohttpClientMocker
225+ ) -> None :
226+ """Test that charset in Content-Type overrides configured encoding."""
227+ # Server sends UTF-8 with correct charset header
228+ aioclient_mock .get (
229+ "http://localhost" ,
230+ status = HTTPStatus .OK ,
231+ content = "Björk Guðmundsdóttir" .encode (),
232+ headers = {"Content-Type" : "text/plain; charset=utf-8" },
233+ )
234+ assert await async_setup_component (
235+ hass ,
236+ SENSOR_DOMAIN ,
237+ {
238+ SENSOR_DOMAIN : {
239+ "name" : "mysensor" ,
240+ "encoding" : "iso-8859-1" , # Config says ISO-8859-1, but charset=utf-8 should win
241+ "platform" : DOMAIN ,
242+ "resource" : "http://localhost" ,
243+ "method" : "GET" ,
244+ }
245+ },
246+ )
247+ await hass .async_block_till_done ()
248+ assert len (hass .states .async_all (SENSOR_DOMAIN )) == 1
249+ # This should work because charset=utf-8 overrides the iso-8859-1 config
250+ assert hass .states .get ("sensor.mysensor" ).state == "Björk Guðmundsdóttir"
251+
252+
165253@pytest .mark .parametrize (
166254 ("ssl_cipher_list" , "ssl_cipher_list_expected" ),
167255 [
0 commit comments