Skip to content

Commit

Permalink
Generate random tagId used to start transaction from UI (#1302)
Browse files Browse the repository at this point in the history
Instead of disabling authorization for start_transaction initiated
via HA user, use a random generated tag and authorize it to start
transaction
  • Loading branch information
rinigus authored Sep 8, 2024
1 parent 1d3e5fe commit 93b0cc8
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import json
import logging
from math import sqrt
import secrets
import ssl
import string
import time

from homeassistant.components.persistent_notification import DOMAIN as PN_DOMAIN
Expand Down Expand Up @@ -378,6 +380,8 @@ def __init__(
self._metrics[csess.meter_start.value].unit = UnitOfMeasure.kwh.value
self._attr_supported_features = prof.NONE
self._metrics[cstat.reconnects.value].value: int = 0
alphabet = string.ascii_uppercase + string.digits
self._remote_id_tag = "".join(secrets.choice(alphabet) for i in range(20))

async def post_connect(self):
"""Logic to be executed right after a charger connects."""
Expand Down Expand Up @@ -766,17 +770,9 @@ async def set_availability(self, state: bool = True):
return False

async def start_transaction(self):
"""
Remote start a transaction.
Check if authorisation enabled, if it is disable it before remote start
"""
resp = await self.get_configuration(ckey.authorize_remote_tx_requests.value)
if resp is True:
await self.configure(ckey.authorize_remote_tx_requests.value, "false")
req = call.RemoteStartTransaction(
connector_id=1, id_tag=self._metrics[cdet.identifier.value].value[:20]
)
"""Remote start a transaction."""
_LOGGER.info("Start transaction with remote ID tag: %s", self._remote_id_tag)
req = call.RemoteStartTransaction(connector_id=1, id_tag=self._remote_id_tag)
resp = await self.call(req)
if resp.status == RemoteStartStopStatus.accepted:
return True
Expand Down Expand Up @@ -1395,6 +1391,9 @@ def on_security_event(self, type, timestamp, **kwargs):

def get_authorization_status(self, id_tag):
"""Get the authorization status for an id_tag."""
# authorize if its the tag of this charger used for remote start_transaction
if id_tag == self._remote_id_tag:
return AuthorizationStatus.accepted.value
# get the domain wide configuration
config = self.hass.data[DOMAIN].get(CONFIG, {})
# get the default authorization status. Use accept if not configured
Expand Down

0 comments on commit 93b0cc8

Please sign in to comment.