generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from nickknissen/ft-services
Add start/stop charging service
- Loading branch information
Showing
5 changed files
with
118 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Monta components services.""" | ||
|
||
import logging | ||
from collections.abc import Awaitable, Callable | ||
|
||
import voluptuous as vol | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, ServiceCall | ||
|
||
from .const import DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
TServiceHandler = Callable[[ServiceCall], Awaitable[None]] | ||
|
||
has_id_schema = vol.Schema({vol.Required("charge_point_id"): int}) | ||
|
||
|
||
async def async_setup_services(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
"""Set up services for the Monta component.""" | ||
|
||
_LOGGER.debug("Set up services") | ||
|
||
coordinator = hass.data[DOMAIN][entry.entry_id] | ||
|
||
async def service_handle_stop_charging(service_call: ServiceCall) -> None: | ||
charge_point_id = service_call.data["charge_point_id"] | ||
_LOGGER.debug("Called stop charging for %s", charge_point_id) | ||
|
||
if coordinator.data[charge_point_id]["state"].startswith("busy"): | ||
await coordinator.async_stop_charge(charge_point_id) | ||
return | ||
|
||
raise vol.Invalid("Charger not currently charging") | ||
|
||
async def service_handle_start_charging(service_call: ServiceCall) -> None: | ||
charge_point_id = service_call.data["charge_point_id"] | ||
_LOGGER.debug("Called start charging for %s", charge_point_id) | ||
|
||
if coordinator.data[charge_point_id]["state"] == "available": | ||
await coordinator.async_start_charge(charge_point_id) | ||
return | ||
|
||
raise vol.Invalid("Charger not currently charging") | ||
|
||
# LIST OF SERVICES | ||
services: list[tuple[str, vol.Schema, TServiceHandler]] = [ | ||
("start_charging", has_id_schema, service_handle_start_charging), | ||
("stop_charging", has_id_schema, service_handle_stop_charging), | ||
] | ||
|
||
# Register the services | ||
for name, schema, handler in services: | ||
hass.services.async_register(DOMAIN, name, handler, schema=schema) |
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,15 @@ | ||
start_charging: | ||
description: "Start charging" | ||
fields: | ||
charge_point_id: | ||
required: true | ||
description: "The charger point id" | ||
example: 1234 | ||
|
||
stop_charging: | ||
description: "Stop charging" | ||
fields: | ||
charge_point_id: | ||
required: true | ||
description: "The charger point id" | ||
example: 1234 |
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 |
---|---|---|
@@ -1,18 +1,41 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"description": "Client id and secret are obtained from https://portal2.monta.app/applications", | ||
"data": { | ||
"client_id": "Client id", | ||
"client_secret": "client secret" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"auth": "Client id/client secret is wrong.", | ||
"connection": "Unable to connect to the server.", | ||
"unknown": "Unknown error occurred." | ||
} | ||
} | ||
} | ||
"config": { | ||
"step": { | ||
"user": { | ||
"description": "Client id and secret are obtained from https://portal2.monta.app/applications", | ||
"data": { | ||
"client_id": "Client id", | ||
"client_secret": "client secret" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"auth": "Client id/client secret is wrong.", | ||
"connection": "Unable to connect to the server.", | ||
"unknown": "Unknown error occurred." | ||
} | ||
}, | ||
"services": { | ||
"start_charging": { | ||
"description": "Start charge on selected charger", | ||
"name": "Start charging", | ||
"fields": { | ||
"charge_point_id": { | ||
"name": "Charge point id", | ||
"description": "The ID of the charger." | ||
} | ||
|
||
} | ||
}, | ||
"stop_charging": { | ||
"description": "Stop charge on selected charger.", | ||
"name": "Stop charging", | ||
"fields": { | ||
"charge_point_id": { | ||
"name": "Charge point id", | ||
"description": "The ID of the charger." | ||
} | ||
} | ||
} | ||
} | ||
} |