Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Scrape sensor unavailable when errors #134143

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion homeassistant/components/scrape/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def _extract_value(self) -> Any:
"""Parse the html extraction in the executor."""
raw_data = self.coordinator.data
value: str | list[str] | None
self._attr_available = True
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved
try:
if self._attr is not None:
value = raw_data.select(self._select)[self._index][self._attr]
Expand All @@ -184,11 +185,13 @@ def _extract_value(self) -> Any:
except IndexError:
_LOGGER.warning("Index '%s' not found in %s", self._index, self.entity_id)
value = None
self._attr_available = False
except KeyError:
_LOGGER.warning(
"Attribute '%s' not found in %s", self._attr, self.entity_id
)
value = None
self._attr_available = False
_LOGGER.debug("Parsed value: %s", value)
return value

Expand Down Expand Up @@ -224,7 +227,7 @@ def available(self) -> bool:
"""Return if entity is available."""
available1 = CoordinatorEntity.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
def _handle_coordinator_update(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/components/scrape/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ async def test_scrape_sensor_errors(hass: HomeAssistant) -> None:
await hass.async_block_till_done()

state = hass.states.get("sensor.ha_class")
assert state.state == STATE_UNKNOWN
assert state.state == STATE_UNAVAILABLE
state2 = hass.states.get("sensor.ha_class2")
assert state2.state == STATE_UNKNOWN
assert state2.state == STATE_UNAVAILABLE


async def test_scrape_sensor_unique_id(
Expand Down