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.
Refactor start/stop charge into its own module
- Loading branch information
1 parent
e5a7a3b
commit 51a1049
Showing
5 changed files
with
76 additions
and
42 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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
start_charge: | ||
target: | ||
device: | ||
integration: monta | ||
entity: | ||
integration: monta | ||
domain: binary_sensor | ||
start_charging: | ||
description: "Start charging" | ||
fields: | ||
charge_point_id: | ||
required: true | ||
description: "The charger point id" | ||
example: 1234 | ||
|
||
stop_charge: | ||
target: | ||
device: | ||
integration: monta | ||
entity: | ||
integration: monta | ||
domain: binary_sensor | ||
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