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

Update CWR History Service and refactoring/bug fixes #62

Merged
merged 6 commits into from
Feb 4, 2024
Merged
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
Next Next commit
First draft of update history service
faanskit committed Feb 3, 2024
commit 16ddb952e8ce19992d64250b9ff400f268bb5d9c
41 changes: 40 additions & 1 deletion custom_components/checkwatt/__init__.py
Original file line number Diff line number Diff line change
@@ -3,18 +3,26 @@
from __future__ import annotations

import asyncio
import datetime
from datetime import time, timedelta
import logging
import random
from typing import TypedDict

import aiohttp
from pycheckwatt import CheckwattManager
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.core import (
HomeAssistant,
ServiceCall,
ServiceResponse,
SupportsResponse,
)
from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt as dt_util
@@ -36,6 +44,14 @@

PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.EVENT]

UPDATE_HISTORY_SERVICE_NAME = "update_history"
UPDATE_HISTORY_SCHEMA = vol.Schema(
{
vol.Required("start_date"): cv.date,
vol.Required("end_date"): cv.date,
}
)


class CheckwattResp(TypedDict):
"""API response."""
@@ -98,6 +114,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
entry.async_on_unload(entry.add_update_listener(update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async def update_history_items(call: ServiceCall) -> ServiceResponse:
"""Fetch historical data from EIB and Update CheckWattRank."""
# items = await my_client.search(call.data["start"], call.data["end"])
_LOGGER.debug(
"Calling update_history service with start date: %s and end date %s",
call.data["start_date"],
call.data["end_date"],
)
return {
"start_date": call.data["start_date"],
"end_date": call.data["end_date"],
"num_items": 0,
}

hass.services.async_register(
DOMAIN,
UPDATE_HISTORY_SERVICE_NAME,
update_history_items,
schema=UPDATE_HISTORY_SCHEMA,
supports_response=SupportsResponse.ONLY,
)

return True


18 changes: 18 additions & 0 deletions custom_components/checkwatt/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
update_history:
name: "Update CheckWattRank History"
description: "Updates CheckWattRank with historical data from EnergyInBalances."
fields:
start_date:
name: "Start date"
description: "The start date to fetch history from"
required: true
example: "2023-12-01"
selector:
date:
end_date:
name: "End date"
description: "The end date to ftech history from"
required: true
example: "2024-01-01"
selector:
date: