Skip to content

Commit

Permalink
breaking change: remove deprecated Action items and update docs (#694)
Browse files Browse the repository at this point in the history
### Changes included in this PR 

***Breaking change***
As per the warning in 1.0.0 this PR removes deprecated Action items from
the `enums.py` files to coincide with the 2.0.0 release, updates code
references and updates documentation to be consistent with the change
including string references.

For example:
- replaces deprecated Action enums such as `Action.BootNotification`
with `Action.boot_notification`
- replaces string references such as `@on("BootNotification")` and
`action="BootNotification"` with `Action.boot_notification`

If users of the library have not already migrated to the new Action
enums they will need to do so before using 2.0.0.

### Checklist

1. [x] Does your submission pass the existing tests?
2. [x] Are there new tests that cover these additions/changes? 
3. [x] Have you linted your code locally before submission?
  • Loading branch information
drc38 authored Dec 19, 2024
1 parent 9e40584 commit d25580c
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 180 deletions.
2 changes: 1 addition & 1 deletion docs/source/central_system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Remove the `on_connect()` handler from the code above and replace it by the foll
class MyChargePoint(cp):
@on(Action.BootNotification)
@on(Action.boot_notification)
async def on_boot_notification(self, charge_point_vendor, charge_point_model, **kwargs):
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(),
Expand Down
2 changes: 1 addition & 1 deletion examples/v16/central_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class ChargePoint(cp):
@on(Action.BootNotification)
@on(Action.boot_notification)
def on_boot_notification(
self, charge_point_vendor: str, charge_point_model: str, **kwargs
):
Expand Down
5 changes: 3 additions & 2 deletions examples/v201/central_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
from ocpp.routing import on
from ocpp.v201 import ChargePoint as cp
from ocpp.v201 import call_result
from ocpp.v201.enums import Action

logging.basicConfig(level=logging.INFO)


class ChargePoint(cp):
@on("BootNotification")
@on(Action.boot_notification)
def on_boot_notification(self, charging_station, reason, **kwargs):
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(), interval=10, status="Accepted"
)

@on("Heartbeat")
@on(Action.heartbeat)
def on_heartbeat(self):
print("Got a Heartbeat!")
return call_result.HeartbeatPayload(
Expand Down
10 changes: 6 additions & 4 deletions ocpp/charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,22 @@ def _raise_key_error(action, version):
from ocpp.v201.enums import Action as v201_Action

if version == "1.6":
if hasattr(v16_Action, action):
try:
v16_Action(action)
raise NotImplementedError(
details={"cause": f"No handler for {action} registered."}
)
else:
except ValueError:
raise NotSupportedError(
details={"cause": f"{action} not supported by OCPP{version}."}
)
elif version in ["2.0", "2.0.1"]:
if hasattr(v201_Action, action):
try:
v201_Action(action)
raise NotImplementedError(
details={"cause": f"No handler for {action} registered."}
)
else:
except ValueError:
raise NotSupportedError(
details={"cause": f"{action} not supported by OCPP{version}."}
)
Expand Down
10 changes: 5 additions & 5 deletions ocpp/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def on(action, *, skip_schema_validation=False):
```
class MyChargePoint(cp):
@on(Action.BootNotification):
@on(Action.boot_notification):
async def on_boot_notification(
self,
charge_point_model,
Expand Down Expand Up @@ -64,7 +64,7 @@ def after(action):
It can be used like so:
@after(Action.BootNotification):
@after(Action.boot_notification):
def after_boot_notification():
pass
Expand Down Expand Up @@ -93,19 +93,19 @@ def create_route_map(obj):
class ChargePoint:
@on(Action.BootNotification)
@on(Action.boot_notification)
def on_boot_notification(self, *args, **kwargs):
pass
@after(Action.BootNotification)
@after(Action.boot_notification)
def after_boot_notification(self, *args, **kwargs):
pass
In this case this returns:
{
Action.BootNotification: {
Action.boot_notification: {
'_on_action': <reference to 'on_boot_notification'>,
'_after_action': <reference to 'after_boot_notification'>,
'_skip_schema_validation': False,
Expand Down
52 changes: 0 additions & 52 deletions ocpp/v16/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from warnings import warn

try:
# breaking change introduced in python 3.11
from enum import StrEnum
Expand All @@ -13,56 +11,6 @@ class StrEnum(str, Enum): # pragma: no cover
class Action(StrEnum):
"""An Action is a required part of a Call message."""

def __init__(self, *args, **kwargs):
warn(
message="Action enum contains deprecated members and will be removed in "
"the next major release, please use snake case members.",
category=DeprecationWarning,
)

# --------- Soon to be deprecated ---------------------
Authorize = "Authorize"
BootNotification = "BootNotification"
CancelReservation = "CancelReservation"
CertificateSigned = "CertificateSigned"
ChangeAvailability = "ChangeAvailability"
ChangeConfiguration = "ChangeConfiguration"
ClearCache = "ClearCache"
ClearChargingProfile = "ClearChargingProfile"
DataTransfer = "DataTransfer"
DeleteCertificate = "DeleteCertificate"
DiagnosticsStatusNotification = "DiagnosticsStatusNotification"
ExtendedTriggerMessage = "ExtendedTriggerMessage"
FirmwareStatusNotification = "FirmwareStatusNotification"
GetCompositeSchedule = "GetCompositeSchedule"
GetConfiguration = "GetConfiguration"
GetDiagnostics = "GetDiagnostics"
GetInstalledCertificateIds = "GetInstalledCertificateIds"
GetLocalListVersion = "GetLocalListVersion"
GetLog = "GetLog"
Heartbeat = "Heartbeat"
InstallCertificate = "InstallCertificate"
LogStatusNotification = "LogStatusNotification"
MeterValues = "MeterValues"
RemoteStartTransaction = "RemoteStartTransaction"
RemoteStopTransaction = "RemoteStopTransaction"
ReserveNow = "ReserveNow"
Reset = "Reset"
SecurityEventNotification = "SecurityEventNotification"
SendLocalList = "SendLocalList"
SetChargingProfile = "SetChargingProfile"
SignCertificate = "SignCertificate"
SignedFirmwareStatusNotification = "SignedFirmwareStatusNotification"
SignedUpdateFirmware = "SignedUpdateFirmware"
StartTransaction = "StartTransaction"
StatusNotification = "StatusNotification"
StopTransaction = "StopTransaction"
TriggerMessage = "TriggerMessage"
UnlockConnector = "UnlockConnector"
UpdateFirmware = "UpdateFirmware"

# --------------------------------------------------------

authorize = "Authorize"
boot_notification = "BootNotification"
cancel_reservation = "CancelReservation"
Expand Down
76 changes: 0 additions & 76 deletions ocpp/v201/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from warnings import warn

try:
# breaking change introduced in python 3.11
from enum import StrEnum
Expand All @@ -13,80 +11,6 @@ class StrEnum(str, Enum): # pragma: no cover
class Action(StrEnum):
"""An Action is a required part of a Call message."""

def __init__(self, *args, **kwargs):
warn(
message="Action enum contains deprecated members and will be removed in "
"the next major release, please use snake case members.",
category=DeprecationWarning,
)

# --------- Soon to be deprecated ---------------------
Authorize = "Authorize"
BootNotification = "BootNotification"
CancelReservation = "CancelReservation"
CertificateSigned = "CertificateSigned"
ChangeAvailability = "ChangeAvailability"
ClearCache = "ClearCache"
ClearChargingProfile = "ClearChargingProfile"
ClearDisplayMessage = "ClearDisplayMessage"
ClearedChargingLimit = "ClearedChargingLimit"
ClearVariableMonitoring = "ClearVariableMonitoring"
CostUpdated = "CostUpdated"
CustomerInformation = "CustomerInformation"
DataTransfer = "DataTransfer"
DeleteCertificate = "DeleteCertificate"
FirmwareStatusNotification = "FirmwareStatusNotification"
Get15118EVCertificate = "Get15118EVCertificate"
GetBaseReport = "GetBaseReport"
GetCertificateStatus = "GetCertificateStatus"
GetChargingProfiles = "GetChargingProfiles"
GetCompositeSchedule = "GetCompositeSchedule"
GetDisplayMessages = "GetDisplayMessages"
GetInstalledCertificateIds = "GetInstalledCertificateIds"
GetLocalListVersion = "GetLocalListVersion"
GetLog = "GetLog"
GetMonitoringReport = "GetMonitoringReport"
GetReport = "GetReport"
GetTransactionStatus = "GetTransactionStatus"
GetVariables = "GetVariables"
Heartbeat = "Heartbeat"
InstallCertificate = "InstallCertificate"
LogStatusNotification = "LogStatusNotification"
MeterValues = "MeterValues"
NotifyChargingLimit = "NotifyChargingLimit"
NotifyCustomerInformation = "NotifyCustomerInformation"
NotifyDisplayMessages = "NotifyDisplayMessages"
NotifyEVChargingNeeds = "NotifyEVChargingNeeds"
NotifyEVChargingSchedule = "NotifyEVChargingSchedule"
NotifyEvent = "NotifyEvent"
NotifyMonitoringReport = "NotifyMonitoringReport"
NotifyReport = "NotifyReport"
PublishFirmware = "PublishFirmware"
PublishFirmwareStatusNotification = "PublishFirmwareStatusNotification"
ReportChargingProfiles = "ReportChargingProfiles"
RequestStartTransaction = "RequestStartTransaction"
RequestStopTransaction = "RequestStopTransaction"
ReservationStatusUpdate = "ReservationStatusUpdate"
ReserveNow = "ReserveNow"
Reset = "Reset"
SecurityEventNotification = "SecurityEventNotification"
SendLocalList = "SendLocalList"
SetChargingProfile = "SetChargingProfile"
SetDisplayMessage = "SetDisplayMessage"
SetMonitoringBase = "SetMonitoringBase"
SetMonitoringLevel = "SetMonitoringLevel"
SetNetworkProfile = "SetNetworkProfile"
SetVariableMonitoring = "SetVariableMonitoring"
SetVariables = "SetVariables"
SignCertificate = "SignCertificate"
StatusNotification = "StatusNotification"
TransactionEvent = "TransactionEvent"
TriggerMessage = "TriggerMessage"
UnlockConnector = "UnlockConnector"
UnpublishFirmware = "UnpublishFirmware"
UpdateFirmware = "UpdateFirmware"
# --------------------------------------------------------

authorize = "Authorize"
boot_notification = "BootNotification"
cancel_reservation = "CancelReservation"
Expand Down
16 changes: 8 additions & 8 deletions tests/test_charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def foo(self):

def test_multiple_classes_with_same_name_for_handler():
class ChargerA(cp_201):
@on(Action.Heartbeat)
@on(Action.heartbeat)
def heartbeat(self, **kwargs):
pass

class ChargerB(cp_201):
@on(Action.Heartbeat)
@on(Action.heartbeat)
def heartbeat(self, **kwargs):
pass

Expand Down Expand Up @@ -409,7 +409,7 @@ class ChargerA(cp_16):
on_boot_notification_call_count = 0
after_boot_notification_call_count = 0

@on(Action.BootNotification)
@on(Action.boot_notification)
def on_boot_notification(self, *args, **kwargs):
# call_unique_id should not be passed as arg nor kwarg
assert kwargs == camel_to_snake_case(payload_a)
Expand All @@ -419,7 +419,7 @@ def on_boot_notification(self, *args, **kwargs):
current_time="foo", interval=1, status=RegistrationStatus.accepted
)

@after(Action.BootNotification)
@after(Action.boot_notification)
def after_boot_notification(self, call_unique_id, *args, **kwargs):
assert call_unique_id == charger_a_test_call_unique_id
assert kwargs == camel_to_snake_case(payload_a)
Expand All @@ -434,7 +434,7 @@ class ChargerB(cp_16):
on_boot_notification_call_count = 0
after_boot_notification_call_count = 0

@on(Action.BootNotification)
@on(Action.boot_notification)
def on_boot_notification(self, call_unique_id, *args, **kwargs):
assert call_unique_id == charger_b_test_call_unique_id
assert kwargs == camel_to_snake_case(payload_b)
Expand All @@ -445,7 +445,7 @@ def on_boot_notification(self, call_unique_id, *args, **kwargs):
current_time="foo", interval=1, status=RegistrationStatus.accepted
)

@after(Action.BootNotification)
@after(Action.boot_notification)
def after_boot_notification(self, *args, **kwargs):
# call_unique_id should not be passed as arg nor kwarg
assert kwargs == camel_to_snake_case(payload_b)
Expand All @@ -460,14 +460,14 @@ def after_boot_notification(self, *args, **kwargs):

msg_a = Call(
unique_id=charger_a_test_call_unique_id,
action=Action.BootNotification.value,
action=Action.boot_notification.value,
payload=payload_a,
)
await charger_a._handle_call(msg_a)

msg_b = Call(
unique_id=charger_b_test_call_unique_id,
action=Action.BootNotification.value,
action=Action.boot_notification.value,
payload=payload_b,
)
await charger_b._handle_call(msg_b)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TypeConstraintViolationError,
)
from ocpp.messages import Call, _validate_payload
from ocpp.v16.enums import Action


def test_exception_with_error_details():
Expand All @@ -27,7 +28,7 @@ def test_exception_show_triggered_message_type_constraint():
# so this should raise a TypeConstraintViolationError
call = Call(
unique_id=123456,
action="BootNotification",
action=Action.boot_notification,
payload={"chargePointVendor": 1, "chargePointModel": "SingleSocketCharger"},
)
ocpp_message = (
Expand All @@ -44,7 +45,7 @@ def test_exception_show_triggered_message_format():
# The payload is syntactically incorrect, should trigger a FormatViolationError
call = Call(
unique_id=123457,
action="BootNotification",
action=Action.boot_notification,
payload={"syntactically": "incorrect"},
)

Expand Down
Loading

0 comments on commit d25580c

Please sign in to comment.