Skip to content

Commit

Permalink
Refactor start/stop charge into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Sep 25, 2023
1 parent e5a7a3b commit 51a1049
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 42 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
24 changes: 0 additions & 24 deletions custom_components/monta/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import logging

import voluptuous as vol
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
Expand Down Expand Up @@ -42,10 +41,6 @@ async def async_setup_entry(hass, entry, async_add_devices):
for entity_description in ENTITY_DESCRIPTIONS
)

platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service("start_charge", {}, "start_charge")
platform.async_register_entity_service("stop_charge", {}, "stop_charge")


class MontaBinarySensor(MontaEntity, BinarySensorEntity):
"""monta binary_sensor class."""
Expand All @@ -72,22 +67,3 @@ def is_on(self) -> bool:
return self.coordinator.data[self.charge_point_id].get(
self.entity_description.key, False
)

async def start_charge(self):
"""Start charge."""
if (
self.coordinator.data[self.charge_point_id]["state"] == "available"
and self.coordinator.data[self.charge_point_id][self.entity_description.key]
):
await self.coordinator.async_start_charge(self.charge_point_id)
return

raise vol.Invalid("Charger not plugged in and available for charge")

async def stop_charge(self):
"""Stop charge."""
if self.coordinator.data[self.charge_point_id]["state"].startswith("busy"):
await self.coordinator.async_stop_charge(self.charge_point_id)
return

raise vol.Invalid("Charger not currently charging")
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)
28 changes: 14 additions & 14 deletions custom_components/monta/services.yaml
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
8 changes: 4 additions & 4 deletions custom_components/monta/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
}
},
"services": {
"start_charge": {
"start_charging": {
"description": "Start charge on selected charger",
"name": "Start Charge"
"name": "Start charging"
},
"stop_charge": {
"stop_charging": {
"description": "Stop charge on selected charger.",
"name": "Stop Charge"
"name": "Stop charging"
}
}
}

0 comments on commit 51a1049

Please sign in to comment.