Add unique id for Scrape config entry entities (#82508)

* scrape unique id

* fix uuid str

* add back UoM
This commit is contained in:
G Johansson 2022-11-24 21:12:47 +01:00 committed by GitHub
parent e4fbbdfa05
commit a63581b5c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 2 deletions

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Mapping
from typing import Any
import uuid
import voluptuous as vol
@ -24,6 +25,7 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_RESOURCE,
CONF_TIMEOUT,
CONF_UNIQUE_ID,
CONF_UNIT_OF_MEASUREMENT,
CONF_USERNAME,
CONF_VALUE_TEMPLATE,
@ -125,7 +127,15 @@ def validate_rest_setup(user_input: dict[str, Any]) -> dict[str, Any]:
def validate_sensor_setup(user_input: dict[str, Any]) -> dict[str, Any]:
"""Validate sensor setup."""
return {"sensor": [{**user_input, CONF_INDEX: int(user_input[CONF_INDEX])}]}
return {
"sensor": [
{
**user_input,
CONF_INDEX: int(user_input[CONF_INDEX]),
CONF_UNIQUE_ID: str(uuid.uuid1()),
}
]
}
DATA_SCHEMA_RESOURCE = vol.Schema(RESOURCE_SETUP)

View file

@ -163,6 +163,7 @@ async def async_setup_entry(
attr: str | None = sensor_config.get(CONF_ATTRIBUTE)
index: int = int(sensor_config[CONF_INDEX])
value_string: str | None = sensor_config.get(CONF_VALUE_TEMPLATE)
unique_id: str = sensor_config[CONF_UNIQUE_ID]
value_template: Template | None = (
Template(value_string, hass) if value_string is not None else None
@ -173,7 +174,7 @@ async def async_setup_entry(
coordinator,
sensor_config,
name,
None,
unique_id,
select,
attr,
index,

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Any
from unittest.mock import patch
import uuid
import pytest
@ -15,6 +16,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_RESOURCE,
CONF_TIMEOUT,
CONF_UNIQUE_ID,
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant
@ -41,6 +43,7 @@ async def get_config_to_integration_load() -> dict[str, Any]:
CONF_NAME: "Current version",
CONF_SELECT: ".current-version h1",
CONF_INDEX: 0,
CONF_UNIQUE_ID: "3699ef88-69e6-11ed-a1eb-0242ac120002",
}
],
}
@ -78,3 +81,13 @@ async def load_integration(
await hass.async_block_till_done()
return config_entry
@pytest.fixture(autouse=True)
def uuid_fixture() -> str:
"""Automatically path uuid generator."""
with patch(
"homeassistant.components.scrape.config_flow.uuid.uuid1",
return_value=uuid.UUID("3699ef88-69e6-11ed-a1eb-0242ac120002"),
):
yield

View file

@ -18,6 +18,7 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_RESOURCE,
CONF_TIMEOUT,
CONF_UNIQUE_ID,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
@ -78,6 +79,7 @@ async def test_form(hass: HomeAssistant, get_data: MockRestData) -> None:
CONF_NAME: "Current version",
CONF_SELECT: ".current-version h1",
CONF_INDEX: 0.0,
CONF_UNIQUE_ID: "3699ef88-69e6-11ed-a1eb-0242ac120002",
}
],
}
@ -148,6 +150,7 @@ async def test_flow_fails(hass: HomeAssistant, get_data: MockRestData) -> None:
CONF_NAME: "Current version",
CONF_SELECT: ".current-version h1",
CONF_INDEX: 0.0,
CONF_UNIQUE_ID: "3699ef88-69e6-11ed-a1eb-0242ac120002",
}
],
}
@ -192,6 +195,7 @@ async def test_options_flow(hass: HomeAssistant, loaded_entry: MockConfigEntry)
CONF_NAME: "Current version",
CONF_SELECT: ".current-version h1",
CONF_INDEX: 0.0,
CONF_UNIQUE_ID: "3699ef88-69e6-11ed-a1eb-0242ac120002",
}
],
}

View file

@ -414,3 +414,8 @@ async def test_setup_config_entry(
state = hass.states.get("sensor.current_version")
assert state.state == "Current Version: 2021.12.10"
entity_reg = er.async_get(hass)
entity = entity_reg.async_get("sensor.current_version")
assert entity.unique_id == "3699ef88-69e6-11ed-a1eb-0242ac120002"