Skip to content

Commit d453895

Browse files
authored
Change supported features to using flags (#122)
* use flags for supported features * fix enum reference * tweak enum name
1 parent 0aa64dc commit d453895

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

custom_components/ocpp/api.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
HAChargerSession as csess,
7171
HAChargerStatuses as cstat,
7272
OcppMisc as om,
73+
Profiles as prof,
7374
)
7475

7576
_LOGGER: logging.Logger = logging.getLogger(__package__)
@@ -259,14 +260,14 @@ def __init__(
259260
# Indicates if the charger requires a reboot to apply new
260261
# configuration.
261262
self._requires_reboot = False
262-
self._features_supported = {}
263263
self.preparing = asyncio.Event()
264264
self._transactionId = 0
265265
self._metrics = defaultdict(lambda: Metric(None, None))
266266
self._metrics[cdet.identifier.value].value = id
267267
self._metrics[csess.session_time.value].unit = TIME_MINUTES
268268
self._metrics[csess.session_energy.value].unit = UnitOfMeasure.kwh.value
269269
self._metrics[csess.meter_start.value].unit = UnitOfMeasure.kwh.value
270+
self._attr_supported_features: int = 0
270271

271272
async def post_connect(self):
272273
"""Logic to be executed right after a charger connects."""
@@ -331,7 +332,7 @@ async def handle_get_diagnostics(call):
331332
try:
332333
self.status = STATE_OK
333334
await self.get_supported_features()
334-
if om.feature_profile_remote.value in self._features_supported:
335+
if prof.REM in self._attr_supported_features:
335336
await self.trigger_boot_notification()
336337
await self.trigger_status_notification()
337338
await self.become_operative()
@@ -365,7 +366,7 @@ async def handle_get_diagnostics(call):
365366
handle_get_configuration,
366367
GCONF_SERVICE_DATA_SCHEMA,
367368
)
368-
if om.feature_profile_smart.value in self._features_supported:
369+
if prof.SMART in self._attr_supported_features:
369370
self.hass.services.async_register(
370371
DOMAIN, csvcs.service_clear_profile.value, handle_clear_profile
371372
)
@@ -375,7 +376,7 @@ async def handle_get_diagnostics(call):
375376
handle_set_charge_rate,
376377
SCR_SERVICE_DATA_SCHEMA,
377378
)
378-
if om.feature_profile_firmware.value in self._features_supported:
379+
if prof.FW in self._attr_supported_features:
379380
self.hass.services.async_register(
380381
DOMAIN,
381382
csvcs.service_update_firmware.value,
@@ -396,9 +397,20 @@ async def get_supported_features(self):
396397
req = call.GetConfigurationPayload(key=[ckey.supported_feature_profiles.value])
397398
resp = await self.call(req)
398399
for key_value in resp.configuration_key:
399-
self._features_supported = key_value[om.value.value]
400-
self._metrics[cdet.features.value].value = self._features_supported
401-
_LOGGER.debug("Supported feature profiles: %s", self._features_supported)
400+
if om.feature_profile_core.value in key_value[om.value.value]:
401+
self._attr_supported_features |= prof.CORE
402+
if om.feature_profile_firmware.value in key_value[om.value.value]:
403+
self._attr_supported_features |= prof.FW
404+
if om.feature_profile_smart.value in key_value[om.value.value]:
405+
self._attr_supported_features |= prof.SMART
406+
if om.feature_profile_reservation.value in key_value[om.value.value]:
407+
self._attr_supported_features |= prof.RES
408+
if om.feature_profile_remote.value in key_value[om.value.value]:
409+
self._attr_supported_features |= prof.REM
410+
if om.feature_profile_auth.value in key_value[om.value.value]:
411+
self._attr_supported_features |= prof.AUTH
412+
self._metrics[cdet.features.value].value = self._attr_supported_features
413+
_LOGGER.debug("Supported feature profiles: %s", key_value[om.value.value])
402414

403415
async def trigger_boot_notification(self):
404416
"""Trigger a boot notification."""
@@ -441,7 +453,7 @@ async def clear_profile(self):
441453

442454
async def set_charge_rate(self, limit_amps: int = 32, limit_watts: int = 22000):
443455
"""Set a charging profile with defined limit."""
444-
if om.feature_profile_smart.value in self._features_supported:
456+
if prof.SMART in self._attr_supported_features:
445457
resp = await self.get_configuration(
446458
ckey.charging_schedule_allowed_charging_rate_unit.value
447459
)
@@ -511,7 +523,7 @@ async def start_transaction(self, limit_amps: int = 32, limit_watts: int = 22000
511523
resp = await self.get_configuration(ckey.authorize_remote_tx_requests.value)
512524
if resp.lower() == "true":
513525
await self.configure(ckey.authorize_remote_tx_requests.value, "false")
514-
if om.feature_profile_smart.value in self._features_supported:
526+
if prof.SMART in self._attr_supported_features:
515527
resp = await self.get_configuration(
516528
ckey.charging_schedule_allowed_charging_rate_unit.value
517529
)
@@ -593,7 +605,7 @@ async def update_firmware(self, firmware_url: str, wait_time: int = 0):
593605
"""Update charger with new firmware if available."""
594606
"""where firmware_url is the http or https url of the new firmware"""
595607
"""and wait_time is hours from now to wait before install"""
596-
if om.feature_profile_firmware.value in self._features_supported:
608+
if prof.FW in self._attr_supported_features:
597609
schema = vol.Schema(vol.Url())
598610
try:
599611
url = schema(firmware_url)
@@ -612,7 +624,7 @@ async def update_firmware(self, firmware_url: str, wait_time: int = 0):
612624

613625
async def get_diagnostics(self, upload_url: str):
614626
"""Upload diagnostic data to server from charger."""
615-
if om.feature_profile_firmware.value in self._features_supported:
627+
if prof.FW in self._attr_supported_features:
616628
schema = vol.Schema(vol.Url())
617629
try:
618630
url = schema(upload_url)
@@ -963,6 +975,11 @@ def on_heartbeat(self, **kwargs):
963975
self.hass.async_create_task(self.central.update(self.central.cpid))
964976
return call_result.HeartbeatPayload(current_time=now)
965977

978+
@property
979+
def supported_features(self) -> int:
980+
"""Flag of Ocpp features that are supported."""
981+
return self._attr_supported_features
982+
966983
def get_metric(self, measurand: str):
967984
"""Return last known value for given measurand."""
968985
return self._metrics[measurand].value

custom_components/ocpp/enums.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Additional enumerated values to use in home assistant."""
2-
from enum import Enum
2+
from enum import Enum, IntFlag, auto
33

44

55
class HAChargerServices(str, Enum):
@@ -51,6 +51,17 @@ class HAChargerSession(str, Enum):
5151
meter_start = "Meter.Start" # in kWh
5252

5353

54+
class Profiles(IntFlag):
55+
"""Flags to indicate supported feature profiles."""
56+
57+
CORE = auto() # Core
58+
FW = auto() # FirmwareManagement
59+
SMART = auto() # SmartCharging
60+
RES = auto() # Reservation
61+
REM = auto() # RemoteTrigger
62+
AUTH = auto() # LocalAuthListManagement
63+
64+
5465
class OcppMisc(str, Enum):
5566
"""Miscellaneous strings used in ocpp v1.6 responses."""
5667

0 commit comments

Comments
 (0)