Skip to content

Commit d26a0d0

Browse files
authored
Merge pull request #21 from nickknissen/ft-services
Add start/stop charging service
2 parents e158929 + 7c451f3 commit d26a0d0

File tree

5 files changed

+118
-19
lines changed

5 files changed

+118
-19
lines changed

custom_components/monta/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .api import MontaApiClient
1515
from .const import DOMAIN, STORAGE_KEY, STORAGE_VERSION
1616
from .coordinator import MontaDataUpdateCoordinator
17+
from .services import async_setup_services
1718

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

4142
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
43+
44+
await async_setup_services(hass, entry)
45+
4246
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
4347

4448
return True

custom_components/monta/binary_sensor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import logging
55

6-
from homeassistant.helpers.entity import generate_entity_id
76
from homeassistant.components.binary_sensor import (
87
BinarySensorDeviceClass,
98
BinarySensorEntity,
109
BinarySensorEntityDescription,
1110
)
1211

12+
from homeassistant.helpers.entity import generate_entity_id
13+
1314
from .const import DOMAIN
1415
from .coordinator import MontaDataUpdateCoordinator
1516
from .entity import MontaEntity
@@ -63,4 +64,6 @@ def __init__(
6364
@property
6465
def is_on(self) -> bool:
6566
"""Return true if the binary_sensor is on."""
66-
return self.coordinator.data[self.charge_point_id].get(self.entity_description.key, False)
67+
return self.coordinator.data[self.charge_point_id].get(
68+
self.entity_description.key, False
69+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Monta components services."""
2+
3+
import logging
4+
from collections.abc import Awaitable, Callable
5+
6+
import voluptuous as vol
7+
from homeassistant.config_entries import ConfigEntry
8+
from homeassistant.core import HomeAssistant, ServiceCall
9+
10+
from .const import DOMAIN
11+
12+
_LOGGER = logging.getLogger(__name__)
13+
14+
TServiceHandler = Callable[[ServiceCall], Awaitable[None]]
15+
16+
has_id_schema = vol.Schema({vol.Required("charge_point_id"): int})
17+
18+
19+
async def async_setup_services(hass: HomeAssistant, entry: ConfigEntry) -> None:
20+
"""Set up services for the Monta component."""
21+
22+
_LOGGER.debug("Set up services")
23+
24+
coordinator = hass.data[DOMAIN][entry.entry_id]
25+
26+
async def service_handle_stop_charging(service_call: ServiceCall) -> None:
27+
charge_point_id = service_call.data["charge_point_id"]
28+
_LOGGER.debug("Called stop charging for %s", charge_point_id)
29+
30+
if coordinator.data[charge_point_id]["state"].startswith("busy"):
31+
await coordinator.async_stop_charge(charge_point_id)
32+
return
33+
34+
raise vol.Invalid("Charger not currently charging")
35+
36+
async def service_handle_start_charging(service_call: ServiceCall) -> None:
37+
charge_point_id = service_call.data["charge_point_id"]
38+
_LOGGER.debug("Called start charging for %s", charge_point_id)
39+
40+
if coordinator.data[charge_point_id]["state"] == "available":
41+
await coordinator.async_start_charge(charge_point_id)
42+
return
43+
44+
raise vol.Invalid("Charger not currently charging")
45+
46+
# LIST OF SERVICES
47+
services: list[tuple[str, vol.Schema, TServiceHandler]] = [
48+
("start_charging", has_id_schema, service_handle_start_charging),
49+
("stop_charging", has_id_schema, service_handle_stop_charging),
50+
]
51+
52+
# Register the services
53+
for name, schema, handler in services:
54+
hass.services.async_register(DOMAIN, name, handler, schema=schema)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
start_charging:
2+
description: "Start charging"
3+
fields:
4+
charge_point_id:
5+
required: true
6+
description: "The charger point id"
7+
example: 1234
8+
9+
stop_charging:
10+
description: "Stop charging"
11+
fields:
12+
charge_point_id:
13+
required: true
14+
description: "The charger point id"
15+
example: 1234
Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
{
2-
"config": {
3-
"step": {
4-
"user": {
5-
"description": "Client id and secret are obtained from https://portal2.monta.app/applications",
6-
"data": {
7-
"client_id": "Client id",
8-
"client_secret": "client secret"
9-
}
10-
}
11-
},
12-
"error": {
13-
"auth": "Client id/client secret is wrong.",
14-
"connection": "Unable to connect to the server.",
15-
"unknown": "Unknown error occurred."
16-
}
17-
}
18-
}
2+
"config": {
3+
"step": {
4+
"user": {
5+
"description": "Client id and secret are obtained from https://portal2.monta.app/applications",
6+
"data": {
7+
"client_id": "Client id",
8+
"client_secret": "client secret"
9+
}
10+
}
11+
},
12+
"error": {
13+
"auth": "Client id/client secret is wrong.",
14+
"connection": "Unable to connect to the server.",
15+
"unknown": "Unknown error occurred."
16+
}
17+
},
18+
"services": {
19+
"start_charging": {
20+
"description": "Start charge on selected charger",
21+
"name": "Start charging",
22+
"fields": {
23+
"charge_point_id": {
24+
"name": "Charge point id",
25+
"description": "The ID of the charger."
26+
}
27+
28+
}
29+
},
30+
"stop_charging": {
31+
"description": "Stop charge on selected charger.",
32+
"name": "Stop charging",
33+
"fields": {
34+
"charge_point_id": {
35+
"name": "Charge point id",
36+
"description": "The ID of the charger."
37+
}
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)