@@ -701,15 +701,17 @@ class Permissions(StrEnum):
701701 "deserialize" : lambda x : Batteries .Mode .__members__ .get (x .upper (), None )
702702 },
703703 )
704- permissions : list [Permissions ] = field (
705- default_factory = list ,
704+ permissions : list [Permissions ] | None = field (
705+ default = None ,
706706 metadata = {
707707 "deserialize" : lambda lst : [
708708 perm
709709 for item in lst
710710 if (perm := Batteries .Permissions .__members__ .get (item .upper (), None ))
711711 is not None
712712 ]
713+ if lst is not None
714+ else None
713715 },
714716 )
715717 power_w : float = field ()
@@ -720,21 +722,34 @@ class Permissions(StrEnum):
720722
721723 @classmethod
722724 def __post_deserialize__ (cls , obj : Batteries ) -> Batteries :
723- """Set correct mode based on permissions after deserialization."""
725+ """Set correct mode based on permissions after deserialization.
726+ If permissions is missing and mode is ZERO, keep mode as ZERO for backwards compatibility.
727+ If permissions is present (even if empty), apply the mapping logic.
728+ """
724729 # Only adjust if mode is ZERO
725- if obj .mode == cls .Mode .ZERO :
726- perms = set (obj .permissions )
727- if perms == {cls .Permissions .CHARGE_ALLOWED }:
728- obj .mode = cls .Mode .ZERO_CHARGE_ONLY
729- elif perms == {cls .Permissions .DISCHARGE_ALLOWED }:
730- obj .mode = cls .Mode .ZERO_DISCHARGE_ONLY
731- elif perms == {
732- cls .Permissions .CHARGE_ALLOWED ,
733- cls .Permissions .DISCHARGE_ALLOWED ,
734- }:
735- obj .mode = cls .Mode .ZERO
736- elif perms == set ():
737- obj .mode = cls .Mode .STANDBY
730+ if obj .mode != cls .Mode .ZERO :
731+ return obj
732+
733+ # Detect if 'permissions' was present in the original data
734+ # If using mashumaro, the original dict is not available here, so we infer:
735+ # If permissions is None, treat as 'not provided'. If it's an empty list, treat as 'provided but empty'.
736+ if obj .permissions is None :
737+ # Permissions not provided, keep mode as ZERO (backwards compatibility)
738+ return obj
739+
740+ perms = set (obj .permissions )
741+ if perms == {cls .Permissions .CHARGE_ALLOWED }:
742+ obj .mode = cls .Mode .ZERO_CHARGE_ONLY
743+ elif perms == {cls .Permissions .DISCHARGE_ALLOWED }:
744+ obj .mode = cls .Mode .ZERO_DISCHARGE_ONLY
745+ elif perms == {
746+ cls .Permissions .CHARGE_ALLOWED ,
747+ cls .Permissions .DISCHARGE_ALLOWED ,
748+ }:
749+ obj .mode = cls .Mode .ZERO
750+ elif perms == set ():
751+ obj .mode = cls .Mode .STANDBY
752+
738753 return obj
739754
740755
0 commit comments