-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service to refresh data (#28)
NOTE: This is partially implemented.
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
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
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,39 @@ | ||
import logging | ||
from .const import DOMAIN | ||
|
||
from homeassistant.core import HomeAssistant, ServiceCall | ||
from homeassistant.helpers.typing import ConfigType | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: | ||
async def handle_reset_and_refresh_data(call: ServiceCall) -> None: | ||
contract = next(iter(hass.data[DOMAIN]), None) | ||
if not contract: | ||
_LOGGER.error("No contracts available") | ||
return | ||
|
||
coordinator = hass.data[DOMAIN].get(contract).get("coordinator") | ||
if not coordinator: | ||
_LOGGER.error(f"Contract coordinator for {contract} not found") | ||
return | ||
|
||
_LOGGER.warning(f"Performing reset and refresh for {contract}") | ||
|
||
# TODO: Not working - Detected unsafe call not in recorder thread | ||
# await clear_stored_data(hass, coordinator) | ||
await fetch_historic_data(hass, coordinator) | ||
|
||
hass.services.async_register( | ||
DOMAIN, "reset_and_refresh_data", handle_reset_and_refresh_data | ||
) | ||
return True | ||
|
||
|
||
async def clear_stored_data(hass: HomeAssistant, coordinator) -> None: | ||
await coordinator._clear_statistics() | ||
|
||
|
||
async def fetch_historic_data(hass: HomeAssistant, coordinator) -> None: | ||
await coordinator.import_old_consumptions(days=365) |
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,6 @@ | ||
--- | ||
# Service call documentation for Aigues de Barcelona integration | ||
|
||
reset_and_refresh_data: | ||
name: Reset and Refresh Data | ||
description: WARNING! Reset all stored historic/metrics data and start getting metrics from 1 year ago, querying data weekly. |