From a63581b5c857984b4a2c254cd009fa3321149a3c Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 24 Nov 2022 21:12:47 +0100 Subject: [PATCH] Add unique id for Scrape config entry entities (#82508) * scrape unique id * fix uuid str * add back UoM --- homeassistant/components/scrape/config_flow.py | 12 +++++++++++- homeassistant/components/scrape/sensor.py | 3 ++- tests/components/scrape/conftest.py | 13 +++++++++++++ tests/components/scrape/test_config_flow.py | 4 ++++ tests/components/scrape/test_sensor.py | 5 +++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/scrape/config_flow.py b/homeassistant/components/scrape/config_flow.py index 6bb68d2fdec..b5557daff15 100644 --- a/homeassistant/components/scrape/config_flow.py +++ b/homeassistant/components/scrape/config_flow.py @@ -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) diff --git a/homeassistant/components/scrape/sensor.py b/homeassistant/components/scrape/sensor.py index 943cda9564d..e16f6c20f8d 100644 --- a/homeassistant/components/scrape/sensor.py +++ b/homeassistant/components/scrape/sensor.py @@ -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, diff --git a/tests/components/scrape/conftest.py b/tests/components/scrape/conftest.py index c3b63cbb6c1..fa90786ec2f 100644 --- a/tests/components/scrape/conftest.py +++ b/tests/components/scrape/conftest.py @@ -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 diff --git a/tests/components/scrape/test_config_flow.py b/tests/components/scrape/test_config_flow.py index b8a84fd73e9..b097dc7a5a0 100644 --- a/tests/components/scrape/test_config_flow.py +++ b/tests/components/scrape/test_config_flow.py @@ -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", } ], } diff --git a/tests/components/scrape/test_sensor.py b/tests/components/scrape/test_sensor.py index 2f83cdef239..83607f1b993 100644 --- a/tests/components/scrape/test_sensor.py +++ b/tests/components/scrape/test_sensor.py @@ -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"