From dd56cc801060291d75184390bb0a522f3e546bf4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 17 Mar 2021 11:47:42 -1000 Subject: [PATCH] Fix rest sensor data misalignment with multiple sensors (#48043) If there were multiple rest data sources, the index needed to be incremented by type instead of by data source/type --- homeassistant/components/rest/__init__.py | 9 ++- tests/components/rest/test_init.py | 75 +++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/rest/__init__.py b/homeassistant/components/rest/__init__.py index 233a12f44c8..26e8fde57e0 100644 --- a/homeassistant/components/rest/__init__.py +++ b/homeassistant/components/rest/__init__.py @@ -77,6 +77,7 @@ async def _async_process_config(hass, config) -> bool: refresh_tasks = [] load_tasks = [] + platform_idxs = {} for rest_idx, conf in enumerate(config[DOMAIN]): scan_interval = conf.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) resource_template = conf.get(CONF_RESOURCE_TEMPLATE) @@ -91,7 +92,13 @@ async def _async_process_config(hass, config) -> bool: if platform_domain not in conf: continue - for platform_idx, platform_conf in enumerate(conf[platform_domain]): + for platform_conf in conf[platform_domain]: + if platform_domain not in platform_idxs: + platform_idxs[platform_domain] = 0 + else: + platform_idxs[platform_domain] += 1 + platform_idx = platform_idxs[platform_domain] + hass.data[DOMAIN][platform_domain][platform_idx] = platform_conf load = discovery.async_load_platform( diff --git a/tests/components/rest/test_init.py b/tests/components/rest/test_init.py index 19a5651e989..2902addca0c 100644 --- a/tests/components/rest/test_init.py +++ b/tests/components/rest/test_init.py @@ -338,3 +338,78 @@ async def test_reload_fails_to_read_configuration(hass): def _get_fixtures_base_path(): return path.dirname(path.dirname(path.dirname(__file__))) + + +@respx.mock +async def test_multiple_rest_endpoints(hass): + """Test multiple rest endpoints.""" + + respx.get("http://date.jsontest.com").respond( + status_code=200, + json={ + "date": "03-17-2021", + "milliseconds_since_epoch": 1616008268573, + "time": "07:11:08 PM", + }, + ) + + respx.get("http://time.jsontest.com").respond( + status_code=200, + json={ + "date": "03-17-2021", + "milliseconds_since_epoch": 1616008299665, + "time": "07:11:39 PM", + }, + ) + respx.get("http://localhost").respond( + status_code=200, + json={ + "value": "1", + }, + ) + assert await async_setup_component( + hass, + DOMAIN, + { + DOMAIN: [ + { + "resource": "http://date.jsontest.com", + "sensor": [ + { + "name": "JSON Date", + "value_template": "{{ value_json.date }}", + }, + { + "name": "JSON Date Time", + "value_template": "{{ value_json.time }}", + }, + ], + }, + { + "resource": "http://time.jsontest.com", + "sensor": [ + { + "name": "JSON Time", + "value_template": "{{ value_json.time }}", + }, + ], + }, + { + "resource": "http://localhost", + "binary_sensor": [ + { + "name": "Binary Sensor", + "value_template": "{{ value_json.value }}", + }, + ], + }, + ] + }, + ) + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 4 + + assert hass.states.get("sensor.json_date").state == "03-17-2021" + assert hass.states.get("sensor.json_date_time").state == "07:11:08 PM" + assert hass.states.get("sensor.json_time").state == "07:11:39 PM" + assert hass.states.get("binary_sensor.binary_sensor").state == "on"