Skip to content

Commit

Permalink
add authorization list (#437)
Browse files Browse the repository at this point in the history
* add autorization list

* fix linting errors

* fix auth list prcessing

Co-authored-by: lbbrhzn <lbbrhzn>
  • Loading branch information
lbbrhzn authored Mar 16, 2022
1 parent 849fee5 commit a842865
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 11 deletions.
16 changes: 16 additions & 0 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
default_config:

ocpp:
default_authorization_status: 'Invalid'
authorization_list:
- id_tag: '12345678'
name: 'My tag'
authorization_status : Accepted
- id_tag: 'CAFEBABE'
name: 'Some other tag'
authorization_status: Blocked
- id_tag: 'DEADBEEF'
name: 'Old tag'
status: Expired
- id_tag: '12341234'
name: 'Invalid tag'
authorization_status: Invalid

logger:
default: info
logs:
Expand Down
44 changes: 42 additions & 2 deletions custom_components/ocpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,56 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.helpers import device_registry
import homeassistant.helpers.config_validation as cv
import voluptuous as vol

from ocpp.v16.enums import AuthorizationStatus

from .api import CentralSystem
from .const import CONF_CPID, CONF_CSID, DEFAULT_CPID, DEFAULT_CSID, DOMAIN, PLATFORMS
from .const import (
CONF_AUTH_LIST,
CONF_AUTH_STATUS,
CONF_CPID,
CONF_CSID,
CONF_DEFAULT_AUTH_STATUS,
CONF_ID_TAG,
CONF_NAME,
CONFIG,
DEFAULT_CPID,
DEFAULT_CSID,
DOMAIN,
PLATFORMS,
)

_LOGGER: logging.Logger = logging.getLogger(__package__)
logging.getLogger(DOMAIN).setLevel(logging.DEBUG)

AUTH_LIST_SCHEMA = vol.Schema(
{
vol.Required(CONF_ID_TAG): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_AUTH_STATUS): cv.string,
}
)

CONFIG_SCHEMA = vol.Schema(
{
vol.Optional(
CONF_DEFAULT_AUTH_STATUS, default=AuthorizationStatus.accepted.value
): cv.string,
vol.Optional(CONF_AUTH_LIST, default={}): vol.Schema(
{cv.string: AUTH_LIST_SCHEMA}
),
},
extra=vol.ALLOW_EXTRA,
)


async def async_setup(hass: HomeAssistant, config: Config):
"""Set up this integration using YAML is not supported."""
"""Read configuration from yaml."""
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}
hass.data[DOMAIN][CONFIG] = config
return True


Expand Down
45 changes: 36 additions & 9 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@
)

from .const import (
CONF_AUTH_LIST,
CONF_AUTH_STATUS,
CONF_CPID,
CONF_CSID,
CONF_DEFAULT_AUTH_STATUS,
CONF_HOST,
CONF_ID_TAG,
CONF_IDLE_INTERVAL,
CONF_METER_INTERVAL,
CONF_MONITORED_VARIABLES,
Expand All @@ -61,6 +65,7 @@
CONF_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT,
CONF_WEBSOCKET_PING_TRIES,
CONFIG,
DEFAULT_CPID,
DEFAULT_CSID,
DEFAULT_ENERGY_UNIT,
Expand Down Expand Up @@ -1200,16 +1205,38 @@ def on_authorize(self, id_tag, **kwargs):
@on(Action.StartTransaction)
def on_start_transaction(self, connector_id, id_tag, meter_start, **kwargs):
"""Handle a Start Transaction request."""
self._metrics[cstat.id_tag.value].value = id_tag
self.active_transaction_id = int(time.time())
self._metrics[cstat.stop_reason.value].value = ""
self._metrics[csess.transaction_id.value].value = self.active_transaction_id
self._metrics[csess.meter_start.value].value = int(meter_start) / 1000
self.hass.async_create_task(self.central.update(self.central.cpid))
return call_result.StartTransactionPayload(
id_tag_info={om.status.value: AuthorizationStatus.accepted.value},
transaction_id=self.active_transaction_id,
# get the domain wide configuration
config = self.hass.data[DOMAIN].get(CONFIG, {})
# get the default authorization status. Use accept if not configured
default_auth_status = config.get(
CONF_DEFAULT_AUTH_STATUS, AuthorizationStatus.accepted.value
)
# get the authorization list
auth_list = config.get(CONF_AUTH_LIST, {})
# search for the entry, based on the id_tag
auth_status = default_auth_status
for auth_entry in auth_list:
if id_tag is auth_entry.get(CONF_ID_TAG, None):
# get the authorization status, use the default if not configured
auth_status = auth_entry.get(CONF_AUTH_STATUS, default_auth_status)
break

if auth_status is AuthorizationStatus.accepted.value:
self.active_transaction_id = int(time.time())
self._metrics[cstat.id_tag.value].value = id_tag
self._metrics[cstat.stop_reason.value].value = ""
self._metrics[csess.transaction_id.value].value = self.active_transaction_id
self._metrics[csess.meter_start.value].value = int(meter_start) / 1000
result = call_result.StartTransactionPayload(
id_tag_info={om.status.value: AuthorizationStatus.accepted.value},
transaction_id=self.active_transaction_id,
)
else:
result = call_result.StartTransactionPayload(
id_tag_info={om.status.value: auth_status}, transaction_id=0
)
self.hass.async_create_task(self.central.update(self.central.cpid))
return result

@on(Action.StopTransaction)
def on_stop_transaction(self, meter_stop, timestamp, transaction_id, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions custom_components/ocpp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from ocpp.v16.enums import Measurand, UnitOfMeasure

CONF_AUTH_LIST = "authorization_list"
CONF_AUTH_STATUS = "authorization_status"
CONF_CPI = "charge_point_identity"
CONF_CPID = "cpid"
CONF_CSID = "csid"
CONF_DEFAULT_AUTH_STATUS = "default_authorization_status"
CONF_HOST = ha.CONF_HOST
CONF_ID_TAG = "id_tag"
CONF_ICON = ha.CONF_ICON
CONF_IDLE_INTERVAL = "idle_interval"
CONF_MAX_CURRENT = "max_current"
Expand Down Expand Up @@ -41,6 +45,7 @@
DEFAULT_WEBSOCKET_PING_INTERVAL = 20
DEFAULT_WEBSOCKET_PING_TIMEOUT = 20
DOMAIN = "ocpp"
CONFIG = "config"
ICON = "mdi:ev-station"
SLEEP_TIME = 60

Expand Down

0 comments on commit a842865

Please sign in to comment.