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
This commit is contained in:
J. Nick Koston 2021-03-17 11:47:42 -10:00 committed by GitHub
parent fabd73f08b
commit dd56cc8010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 1 deletions

View file

@ -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(

View file

@ -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"