-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list_orphaned_database_entities service (#752)
Co-authored-by: Franck Nijhof <[email protected]>
- Loading branch information
1 parent
e7e1600
commit f8f0335
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
custom_components/spook/ectoplasms/homeassistant/services/list_orphaned_database_entities.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Spook - Your homie.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from sqlalchemy import create_engine, text | ||
|
||
from homeassistant.components.homeassistant import DOMAIN | ||
from homeassistant.components.recorder import ( | ||
get_instance, | ||
) | ||
from homeassistant.core import ServiceResponse, SupportsResponse | ||
|
||
from ....services import AbstractSpookService | ||
|
||
if TYPE_CHECKING: | ||
from homeassistant.core import ServiceCall | ||
|
||
|
||
class SpookService(AbstractSpookService): | ||
"""Home Assistant Core integration service to list all orphaned database entities.""" | ||
|
||
domain = DOMAIN | ||
service = "list_orphaned_database_entities" | ||
supports_response = SupportsResponse.ONLY | ||
|
||
async def async_handle_service(self, call: ServiceCall) -> ServiceResponse: | ||
"""Handle the service call.""" | ||
query = text( | ||
""" | ||
SELECT DISTINCT(entity_id) FROM states_meta | ||
""" | ||
) | ||
db_url = get_instance(self.hass).db_url | ||
engine = create_engine(db_url) | ||
with engine.connect() as conn: | ||
response = conn.execute(query) | ||
db_list = [e[0] for e in response] | ||
states_list = self.hass.states.async_entity_ids() | ||
compared_list = set(db_list).difference(states_list) | ||
if call.return_response: | ||
return { | ||
"count": len(compared_list), | ||
"entities": list(compared_list), | ||
} | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.