mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Update buienradar sensors only after being added to HA (#131830)
* Update buienradar sensors only after being added to HA * Move check to util * Check for platform in sensor state property * Move check to unit translation key property * Add test for sensor check * Properly handle added_to_hass * Remove redundant comment
This commit is contained in:
parent
99063ba141
commit
6db8fced60
@ -742,6 +742,7 @@ class BrSensor(SensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
self._data: BrData | None = None
|
||||||
self._measured = None
|
self._measured = None
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"{coordinates[CONF_LATITUDE]:2.6f}{coordinates[CONF_LONGITUDE]:2.6f}"
|
f"{coordinates[CONF_LATITUDE]:2.6f}{coordinates[CONF_LONGITUDE]:2.6f}"
|
||||||
@ -756,17 +757,29 @@ class BrSensor(SensorEntity):
|
|||||||
if description.key.startswith(PRECIPITATION_FORECAST):
|
if description.key.startswith(PRECIPITATION_FORECAST):
|
||||||
self._timeframe = None
|
self._timeframe = None
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Handle entity being added to hass."""
|
||||||
|
if self._data is None:
|
||||||
|
return
|
||||||
|
self._update()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def data_updated(self, data: BrData):
|
def data_updated(self, data: BrData):
|
||||||
"""Update data."""
|
"""Handle data update."""
|
||||||
if self._load_data(data.data) and self.hass:
|
self._data = data
|
||||||
|
if not self.hass:
|
||||||
|
return
|
||||||
|
self._update()
|
||||||
|
|
||||||
|
def _update(self):
|
||||||
|
"""Update sensor data."""
|
||||||
|
_LOGGER.debug("Updating sensor %s", self.entity_id)
|
||||||
|
if self._load_data(self._data.data):
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _load_data(self, data): # noqa: C901
|
def _load_data(self, data): # noqa: C901
|
||||||
"""Load the sensor with relevant data."""
|
"""Load the sensor with relevant data."""
|
||||||
# Find sensor
|
|
||||||
|
|
||||||
# Check if we have a new measurement,
|
# Check if we have a new measurement,
|
||||||
# otherwise we do not have to update the sensor
|
# otherwise we do not have to update the sensor
|
||||||
if self._measured == data.get(MEASURED):
|
if self._measured == data.get(MEASURED):
|
||||||
|
@ -472,6 +472,11 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
|||||||
"""Return translation key for unit of measurement."""
|
"""Return translation key for unit of measurement."""
|
||||||
if self.translation_key is None:
|
if self.translation_key is None:
|
||||||
return None
|
return None
|
||||||
|
if self.platform is None:
|
||||||
|
raise ValueError(
|
||||||
|
f"Sensor {type(self)} cannot have a translation key for "
|
||||||
|
"unit of measurement before being added to the entity platform"
|
||||||
|
)
|
||||||
platform = self.platform
|
platform = self.platform
|
||||||
return (
|
return (
|
||||||
f"component.{platform.platform_name}.entity.{platform.domain}"
|
f"component.{platform.platform_name}.entity.{platform.domain}"
|
||||||
|
@ -545,6 +545,45 @@ async def test_translated_unit_with_native_unit_raises(
|
|||||||
assert entity0.entity_id is None
|
assert entity0.entity_id is None
|
||||||
|
|
||||||
|
|
||||||
|
async def test_unit_translation_key_without_platform_raises(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test that unit translation key property raises if the entity has no platform yet."""
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.helpers.service.translation.async_get_translations",
|
||||||
|
return_value={
|
||||||
|
"component.test.entity.sensor.test_translation_key.unit_of_measurement": "Tests"
|
||||||
|
},
|
||||||
|
):
|
||||||
|
entity0 = MockSensor(
|
||||||
|
name="Test",
|
||||||
|
native_value="123",
|
||||||
|
unique_id="very_unique",
|
||||||
|
)
|
||||||
|
entity0.entity_description = SensorEntityDescription(
|
||||||
|
"test",
|
||||||
|
translation_key="test_translation_key",
|
||||||
|
)
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError,
|
||||||
|
match="cannot have a translation key for unit of measurement before "
|
||||||
|
"being added to the entity platform",
|
||||||
|
):
|
||||||
|
unit = entity0.unit_of_measurement # noqa: F841
|
||||||
|
|
||||||
|
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass, "sensor", {"sensor": {"platform": "test"}}
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Should not raise after being added to the platform
|
||||||
|
unit = entity0.unit_of_measurement # noqa: F841
|
||||||
|
assert unit == "Tests"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
(
|
(
|
||||||
"device_class",
|
"device_class",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user