mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Set Scrape sensor unavailable when errors (#134143)
This commit is contained in:
parent
e2c59f276a
commit
249d93574a
@ -158,6 +158,7 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
self._index = index
|
self._index = index
|
||||||
self._value_template = value_template
|
self._value_template = value_template
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
|
self._available = True
|
||||||
if not yaml and (unique_id := trigger_entity_config.get(CONF_UNIQUE_ID)):
|
if not yaml and (unique_id := trigger_entity_config.get(CONF_UNIQUE_ID)):
|
||||||
self._attr_name = None
|
self._attr_name = None
|
||||||
self._attr_has_entity_name = True
|
self._attr_has_entity_name = True
|
||||||
@ -172,6 +173,7 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
"""Parse the html extraction in the executor."""
|
"""Parse the html extraction in the executor."""
|
||||||
raw_data = self.coordinator.data
|
raw_data = self.coordinator.data
|
||||||
value: str | list[str] | None
|
value: str | list[str] | None
|
||||||
|
self._available = True
|
||||||
try:
|
try:
|
||||||
if self._attr is not None:
|
if self._attr is not None:
|
||||||
value = raw_data.select(self._select)[self._index][self._attr]
|
value = raw_data.select(self._select)[self._index][self._attr]
|
||||||
@ -184,11 +186,13 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
_LOGGER.warning("Index '%s' not found in %s", self._index, self.entity_id)
|
_LOGGER.warning("Index '%s' not found in %s", self._index, self.entity_id)
|
||||||
value = None
|
value = None
|
||||||
|
self._available = False
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Attribute '%s' not found in %s", self._attr, self.entity_id
|
"Attribute '%s' not found in %s", self._attr, self.entity_id
|
||||||
)
|
)
|
||||||
value = None
|
value = None
|
||||||
|
self._available = False
|
||||||
_LOGGER.debug("Parsed value: %s", value)
|
_LOGGER.debug("Parsed value: %s", value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@ -196,6 +200,7 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
"""Ensure the data from the initial update is reflected in the state."""
|
"""Ensure the data from the initial update is reflected in the state."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self._async_update_from_rest_data()
|
self._async_update_from_rest_data()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def _async_update_from_rest_data(self) -> None:
|
def _async_update_from_rest_data(self) -> None:
|
||||||
"""Update state from the rest data."""
|
"""Update state from the rest data."""
|
||||||
@ -210,21 +215,22 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
SensorDeviceClass.TIMESTAMP,
|
SensorDeviceClass.TIMESTAMP,
|
||||||
}:
|
}:
|
||||||
self._attr_native_value = value
|
self._attr_native_value = value
|
||||||
|
self._attr_available = self._available
|
||||||
self._process_manual_data(raw_value)
|
self._process_manual_data(raw_value)
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_native_value = async_parse_date_datetime(
|
self._attr_native_value = async_parse_date_datetime(
|
||||||
value, self.entity_id, self.device_class
|
value, self.entity_id, self.device_class
|
||||||
)
|
)
|
||||||
|
self._attr_available = self._available
|
||||||
self._process_manual_data(raw_value)
|
self._process_manual_data(raw_value)
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return if entity is available."""
|
"""Return if entity is available."""
|
||||||
available1 = CoordinatorEntity.available.fget(self) # type: ignore[attr-defined]
|
available1 = CoordinatorEntity.available.fget(self) # type: ignore[attr-defined]
|
||||||
available2 = ManualTriggerEntity.available.fget(self) # type: ignore[attr-defined]
|
available2 = ManualTriggerEntity.available.fget(self) # type: ignore[attr-defined]
|
||||||
return bool(available1 and available2)
|
return bool(available1 and available2 and self._attr_available)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
@ -443,9 +443,9 @@ async def test_scrape_sensor_errors(hass: HomeAssistant) -> None:
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("sensor.ha_class")
|
state = hass.states.get("sensor.ha_class")
|
||||||
assert state.state == STATE_UNKNOWN
|
assert state.state == STATE_UNAVAILABLE
|
||||||
state2 = hass.states.get("sensor.ha_class2")
|
state2 = hass.states.get("sensor.ha_class2")
|
||||||
assert state2.state == STATE_UNKNOWN
|
assert state2.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
async def test_scrape_sensor_unique_id(
|
async def test_scrape_sensor_unique_id(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user