Skip to content

Commit

Permalink
Detect unknown source used in trend helpers (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Jan 13, 2024
1 parent 2d55d2a commit 1aabd00
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/spook/ectoplasms/trend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Spook - Not your homie."""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Spook - Not your homie."""
71 changes: 71 additions & 0 deletions custom_components/spook/ectoplasms/trend/repairs/unknown_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Spook - Not your homie."""
from __future__ import annotations

from homeassistant.components import binary_sensor
from homeassistant.const import EVENT_COMPONENT_LOADED
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....repairs import AbstractSpookRepair


class SpookRepair(AbstractSpookRepair):
"""Spook repair tries to find unknown source entites for trend sensors."""

domain = "trend"
repair = "trend_unknown_source"
inspect_events = {
EVENT_COMPONENT_LOADED,
er.EVENT_ENTITY_REGISTRY_UPDATED,
}
inspect_on_reload = "trend"

_issues: set[str] = set()

async def async_inspect(self) -> None:
"""Trigger a inspection."""
LOGGER.debug("Spook is inspecting: %s", self.repair)

platforms: list[EntityPlatform] | None
if not (platforms := self.hass.data[DATA_ENTITY_PLATFORM].get(self.domain)):
return # Nothing to do.

entity_ids = {
entity.entity_id for entity in self.entity_registry.entities.values()
}.union(self.hass.states.async_entity_ids())

possible_issue_ids: set[str] = set()
for platform in platforms:
# We only care about the binary sensor domain
if platform.domain != binary_sensor.DOMAIN:
continue

for entity in platform.entities.values():
possible_issue_ids.add(entity.entity_id)
# pylint: disable-next=protected-access
source = entity._entity_id # noqa: SLF001
if source not in entity_ids:
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"entity_id": entity.entity_id,
"helper": entity.name,
"source": source,
},
)
self._issues.add(entity.entity_id)
LOGGER.debug(
"Spook found unknown source entity %s in %s "
"and created an issue for it",
source,
entity.entity_id,
)
else:
self.async_delete_issue(entity.entity_id)
self._issues.add(entity.entity_id)

# Remove issues that are no longer valid
for issue_id in self._issues - possible_issue_ids:
self.async_delete_issue(issue_id)
self._issues.discard(issue_id)
4 changes: 4 additions & 0 deletions custom_components/spook/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@
"description": "Spook has found a ghost in your utility meter helpers πŸ‘»\n\nWhile floating around, Spook crossed path with the following helper:\n\n{helper} (`{entity_id}`)\n\nThis helper has a source entity unknown to Home Assistant:\n\n`{source}`\n\n\n\nTo fix this error, edit the helper and adjust the source entity (or remove the helper) and restart Home Assistant.\n\nSpook πŸ‘» Not your homie.",
"title": "Unknown source: {helper}"
},
"trend_unknown_source": {
"description": "Spook has found a ghost in your trend helpers πŸ‘»\n\nWhile floating around, Spook crossed path with the following helper:\n\n{helper} (`{entity_id}`)\n\nThis helper has a source entity unknown to Home Assistant:\n\n`{source}`\n\n\n\nTo fix this error, edit the helper and adjust the source entity (or remove the helper) and restart Home Assistant.\n\nSpook πŸ‘» Not your homie.",
"title": "Unknown source: {helper}"
},
"user_issue": {
"fix_flow": {
"step": {
Expand Down
1 change: 1 addition & 0 deletions documentation/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ parts:
- file: integrations/select
- file: integrations/switch_as_x
- file: integrations/timer
- file: integrations/trend
- file: integrations/utility_meter
- file: integrations/zone

Expand Down
5 changes: 5 additions & 0 deletions documentation/enhanced_integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ Spook enhances the following {term}`Home Assistant` {term}`integrations <integra
[![](https://brands.home-assistant.io/timer/icon.png)](integrations/timer)
:::

:::{card} Trend
:footer: πŸ“š [Learn more](integrations/trend)
[![](https://brands.home-assistant.io/trend/icon.png)](integrations/trend)
:::

:::{card} Utility meter
:footer: πŸ“š [Learn more](integrations/utility_meter)
[![](https://brands.home-assistant.io/utility_meter/icon.png)](integrations/utility_meter)
Expand Down
41 changes: 41 additions & 0 deletions documentation/integrations/trend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subject: Enhanced integrations
title: Trend
subtitle: I don't follow trends, trends follow me. 😎
description: Spook enhances the Home Assistant trend integration by report issues in the repairs dashboard.
date: 2024-01-12T20:41:55+01:00
---

```{image} https://brands.home-assistant.io/trend/logo.png
:alt: The Home Assistant trend logo
:width: 250px
:align: center
```

<br><br>

The trend {term}`helper <helper>` integration show the trend of a numeric value over time. It does so by tracking the state of a source entity, such as a sensor, and calculating the trend based on the difference between the current and previous state.

## Devices & entities

Spook does not provide any new devices or entities for this integration.

## Services

Spook does not provide service enhancements for this integration.

## Repairs

While Spook is floating around in your Home Assistant instance, it will raise repairs issues if it has found something that is not right.

### Unknown source entity

Spook inspects all trend sensors created to find source entities they meter that no longer exist. If Spook finds such a case, it will raise a repair issue, informing you about the problematic trend helper and the source entity that is missing.

To resolve the raised issue, you can either remove the trend helper or restore the referenced source entity. Spook will automatically remove the repair issue once the issue is fixed.

## Features requests, ideas, and support

If you have an idea on how to further enhance this integration, for example, by adding a new service, entity, or repairs detection; feel free to [let us know in our discussion forums](https://github.com/frenck/spook/discussions).

Are you stuck using these new features? Or maybe you've run into a bug? Please check the [](../support) page on where to go for help.

0 comments on commit 1aabd00

Please sign in to comment.