Skip to content

Commit

Permalink
Merge pull request #21 from nickknissen/ft-services
Browse files Browse the repository at this point in the history
Add start/stop charging service
  • Loading branch information
nickknissen authored Sep 29, 2023
2 parents e158929 + 7c451f3 commit d26a0d0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 19 deletions.
4 changes: 4 additions & 0 deletions custom_components/monta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .api import MontaApiClient
from .const import DOMAIN, STORAGE_KEY, STORAGE_VERSION
from .coordinator import MontaDataUpdateCoordinator
from .services import async_setup_services

PLATFORMS: list[Platform] = [
Platform.SENSOR,
Expand All @@ -39,6 +40,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator.async_config_entry_first_refresh()

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

await async_setup_services(hass, entry)

entry.async_on_unload(entry.add_update_listener(async_reload_entry))

return True
Expand Down
7 changes: 5 additions & 2 deletions custom_components/monta/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import logging

from homeassistant.helpers.entity import generate_entity_id
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)

from homeassistant.helpers.entity import generate_entity_id

from .const import DOMAIN
from .coordinator import MontaDataUpdateCoordinator
from .entity import MontaEntity
Expand Down Expand Up @@ -63,4 +64,6 @@ def __init__(
@property
def is_on(self) -> bool:
"""Return true if the binary_sensor is on."""
return self.coordinator.data[self.charge_point_id].get(self.entity_description.key, False)
return self.coordinator.data[self.charge_point_id].get(
self.entity_description.key, False
)
54 changes: 54 additions & 0 deletions custom_components/monta/services.py
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)
15 changes: 15 additions & 0 deletions custom_components/monta/services.yaml
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
57 changes: 40 additions & 17 deletions custom_components/monta/translations/en.json
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."
}
}
}
}
}

0 comments on commit d26a0d0

Please sign in to comment.