Remove the unit of measurement null guard from scrape sensor config entries (#188199)

This commit is contained in:
Jan Bouwhuis
2026-10-23 16:24:14 +00:00
committed by GitHub
parent 228316db2e
commit 78b0f2dfde
2 changed files with 3 additions and 71 deletions
+3 -10
View File
@@ -17,7 +17,6 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import (
AddConfigEntryEntitiesCallback,
@@ -53,14 +52,6 @@ TRIGGER_ENTITY_OPTIONS = (
CONF_UNIT_OF_MEASUREMENT,
)
SENSOR_CONFIG_ENTRY_SCHEMA = TEMPLATE_SENSOR_BASE_SCHEMA.extend(
{
# The unit of measurement selector returns None when no unit is set
probatio.Optional(CONF_UNIT_OF_MEASUREMENT): probatio.Any(None, cv.string),
},
extra=probatio.ALLOW_EXTRA,
)
async def async_setup_platform(
hass: HomeAssistant,
@@ -124,7 +115,9 @@ async def async_setup_entry(
sensor[CONF_UNIQUE_ID] = subentry.subentry_id
sensor[CONF_NAME] = subentry.title
sensor_config: ConfigType = SENSOR_CONFIG_ENTRY_SCHEMA(sensor)
sensor_config: ConfigType = probatio.Schema(
TEMPLATE_SENSOR_BASE_SCHEMA.schema, extra=probatio.ALLOW_EXTRA
)(sensor)
value_string: str | None = sensor_config.get(CONF_VALUE_TEMPLATE)
-61
View File
@@ -1,7 +1,6 @@
"""The tests for the Scrape sensor platform."""
from datetime import timedelta
from typing import Any
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
@@ -626,66 +625,6 @@ async def test_setup_config_entry(
assert entity.unique_id == "01JZN07D8D23994A49YKS649S7"
@pytest.mark.parametrize(
("get_sensor_config", "expected_state", "expected_attributes"),
[
pytest.param(
(
{
"data": {
"additional": {"unit_of_measurement": None},
CONF_INDEX: 0,
CONF_SELECT: ".current-version h1",
},
"subentry_id": "01JZN07D8D23994A49YKS649S7",
"subentry_type": "entity",
"title": "Current version",
"unique_id": None,
},
),
"Current Version: 2021.12.10",
{},
id="no_unit",
),
pytest.param(
(
{
"data": {
"additional": {
"device_class": "aqi",
"unit_of_measurement": None,
CONF_VALUE_TEMPLATE: "{{ 42 }}",
},
CONF_INDEX: 0,
CONF_SELECT: ".current-version h1",
},
"subentry_id": "01JZN07D8D23994A49YKS649S7",
"subentry_type": "entity",
"title": "Current version",
"unique_id": None,
},
),
"42",
{"device_class": "aqi"},
id="aqi_no_unit",
),
],
)
async def test_setup_config_entry_without_unit(
hass: HomeAssistant,
loaded_entry: MockConfigEntry,
expected_state: str,
expected_attributes: dict[str, Any],
) -> None:
"""Test setup from config entry when the unit of measurement selector sends None."""
state = hass.states.get("sensor.current_version")
assert state is not None
assert state.state == expected_state
assert "unit_of_measurement" not in state.attributes
for key, value in expected_attributes.items():
assert state.attributes[key] == value
async def test_templates_with_yaml(hass: HomeAssistant) -> None:
"""Test the Scrape sensor from yaml config with templates."""