Skip to content

Commit 51a1049

Browse files
committed
Refactor start/stop charge into its own module
1 parent e5a7a3b commit 51a1049

File tree

5 files changed

+76
-42
lines changed

5 files changed

+76
-42
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: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import logging
55

6-
import voluptuous as vol
76
from homeassistant.components.binary_sensor import (
87
BinarySensorDeviceClass,
98
BinarySensorEntity,
@@ -42,10 +41,6 @@ async def async_setup_entry(hass, entry, async_add_devices):
4241
for entity_description in ENTITY_DESCRIPTIONS
4342
)
4443

45-
platform = entity_platform.async_get_current_platform()
46-
platform.async_register_entity_service("start_charge", {}, "start_charge")
47-
platform.async_register_entity_service("stop_charge", {}, "stop_charge")
48-
4944

5045
class MontaBinarySensor(MontaEntity, BinarySensorEntity):
5146
"""monta binary_sensor class."""
@@ -72,22 +67,3 @@ def is_on(self) -> bool:
7267
return self.coordinator.data[self.charge_point_id].get(
7368
self.entity_description.key, False
7469
)
75-
76-
async def start_charge(self):
77-
"""Start charge."""
78-
if (
79-
self.coordinator.data[self.charge_point_id]["state"] == "available"
80-
and self.coordinator.data[self.charge_point_id][self.entity_description.key]
81-
):
82-
await self.coordinator.async_start_charge(self.charge_point_id)
83-
return
84-
85-
raise vol.Invalid("Charger not plugged in and available for charge")
86-
87-
async def stop_charge(self):
88-
"""Stop charge."""
89-
if self.coordinator.data[self.charge_point_id]["state"].startswith("busy"):
90-
await self.coordinator.async_stop_charge(self.charge_point_id)
91-
return
92-
93-
raise vol.Invalid("Charger not currently charging")
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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
start_charge:
2-
target:
3-
device:
4-
integration: monta
5-
entity:
6-
integration: monta
7-
domain: binary_sensor
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
88

9-
stop_charge:
10-
target:
11-
device:
12-
integration: monta
13-
entity:
14-
integration: monta
15-
domain: binary_sensor
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

custom_components/monta/translations/en.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
}
1717
},
1818
"services": {
19-
"start_charge": {
19+
"start_charging": {
2020
"description": "Start charge on selected charger",
21-
"name": "Start Charge"
21+
"name": "Start charging"
2222
},
23-
"stop_charge": {
23+
"stop_charging": {
2424
"description": "Stop charge on selected charger.",
25-
"name": "Stop Charge"
25+
"name": "Stop charging"
2626
}
2727
}
2828
}

0 commit comments

Comments
 (0)