Skip to content

Commit

Permalink
Added service to push to CWR
Browse files Browse the repository at this point in the history
  • Loading branch information
faanskit committed Feb 4, 2024
1 parent 64fb365 commit db36e59
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions custom_components/checkwatt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
}
)

PUSH_CWR_SERVICE_NAME = "push_checkwatt_rank"
PUSH_CWR_SCHEMA = None


CHECKWATTRANK_REPORTER = "HomeAssistantV2"


Expand Down Expand Up @@ -196,6 +200,53 @@ async def update_history_items(call: ServiceCall) -> ServiceResponse:
"total_items": total_items,
}

async def push_cwr(call: ServiceCall) -> ServiceResponse:
"""Push data to CheckWattRank."""
username = entry.data.get(CONF_USERNAME)
password = entry.data.get(CONF_PASSWORD)
cwr_name = entry.options.get(CONF_CWR_NAME)
status = None
async with CheckwattManager(username, password, INTEGRATION_NAME) as cw:
try:
# Login to EnergyInBalance
if await cw.login():
# Fetch customer detail
if not await cw.get_customer_details():
_LOGGER.error("Failed to fetch customer details")
return {
"status": "Failed to fetch customer details",
}

if not await cw.get_price_zone():
_LOGGER.error("Failed to fetch prize zone")
return {
"status": "Failed to fetch prize zone",
}

if not await cw.get_fcrd_today_net_revenue():
raise UpdateFailed("Unknown error get_fcrd_revenue")

display_name = cwr_name if cwr_name != "" else cw.display_name
if await push_to_checkwatt_rank(
cw, display_name, cw.fcrd_today_net_revenue
):
status = "Data successfully sent to CheckWattRank"
else:
status = "Failed to update to CheckWattRank"

else:
status = "Failed to login."

except InvalidAuth as err:
raise ConfigEntryAuthFailed from err

except CheckwattError as err:
status = f"Failed to update CheckWattRank: {err}"

return {
"result": status,
}

hass.services.async_register(
DOMAIN,
UPDATE_HISTORY_SERVICE_NAME,
Expand All @@ -204,6 +255,14 @@ async def update_history_items(call: ServiceCall) -> ServiceResponse:
supports_response=SupportsResponse.ONLY,
)

hass.services.async_register(
DOMAIN,
PUSH_CWR_SERVICE_NAME,
push_cwr,
schema=PUSH_CWR_SCHEMA,
supports_response=SupportsResponse.ONLY,
)

return True


Expand Down
4 changes: 4 additions & 0 deletions custom_components/checkwatt/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ update_history:
example: "2024-01-01"
selector:
date:

push_checkwatt_rank:
name: "Push Today's Revenue to CheckWattRank"
description: "Updates CheckWattRank with current revenues from EnergyInBalances."

0 comments on commit db36e59

Please sign in to comment.