Skip to content

Commit

Permalink
PowermeterType and RscpError as Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
eikowagenknecht committed Oct 10, 2023
1 parent 19e19f7 commit 5f72752
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions e3dc/_e3dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from ._e3dc_rscp_web import E3DC_RSCP_web
from ._rscpLib import rscpFindTag, rscpFindTagIndex
from ._rscpTags import RscpTag, RscpType, getPowermeterType
from ._rscpTags import RscpTag, RscpType, getStrPowermeterType

REMOTE_ADDRESS = "https://s10.e3dc.com/s10/phpcmd/cmd.php"
REQUEST_INTERVAL_SEC = 10 # minimum interval between requests
Expand Down Expand Up @@ -1904,7 +1904,7 @@ def get_powermeters(self, keepAlive=False):
{
"index": pmIndex,
"type": pmType,
"typeName": getPowermeterType(pmType),
"typeName": getStrPowermeterType(pmType),
}
)

Expand Down
10 changes: 5 additions & 5 deletions e3dc/_rscpLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

from ._rscpTags import (
RscpType,
getErrorcode,
getHexRscpTag,
getHexRscpType,
getRscpType,
getStrRscpTag,
getStrRscpError,
getHexRscpType,
getHexRscpTag,
getStrRscpType,
getStrRscpTag,
)

DEBUG_DICT = {"print_rscp": False}
Expand Down Expand Up @@ -276,7 +276,7 @@ def rscpDecode(data):
val = struct.unpack(fmt, data[headerSize : headerSize + struct.calcsize(fmt)])[0]

if type_ == RscpType.Error:
val = getErrorcode(int.from_bytes(val, "little"))
val = getStrRscpError(int.from_bytes(val, "little"))
elif isinstance(val, bytes) and type_ == RscpType.CString:
# return string instead of bytes
# ignore none utf-8 bytes
Expand Down
46 changes: 29 additions & 17 deletions e3dc/_rscpTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,8 @@ class RscpType(Enum):


class RscpError(Enum):
"""All available RSCP error codes. Imported from https://s10.e3dc.com/s10/js/rscpLibV0.9.3.min.js."""

RSCP_ERR_NOT_HANDLED = 0x01
RSCP_ERR_ACCESS_DENIED = 0x02
RSCP_ERR_FORMAT = 0x03
Expand All @@ -3605,17 +3607,18 @@ class RscpError(Enum):
"""Catch all for unexpected errors. Happens for example in get_db_data if time and span is invalid (not available)."""


powermeterTypes = {
0x00: "PM_TYPE_UNDEFINED",
0x01: "PM_TYPE_ROOT",
0x02: "PM_TYPE_ADDITIONAL",
0x03: "PM_TYPE_ADDITIONAL_PRODUCTION",
0x04: "PM_TYPE_ADDITIONAL_CONSUMPTION",
0x05: "PM_TYPE_FARM",
0x06: "PM_TYPE_UNUSED",
0x07: "PM_TYPE_WALLBOX",
0x08: "PM_TYPE_FARM_ADDITIONAL",
}
class PowermeterType(Enum):
"""All available powermeter types."""

PM_TYPE_UNDEFINED = 0x00
PM_TYPE_ROOT = 0x01
PM_TYPE_ADDITIONAL = 0x02
PM_TYPE_ADDITIONAL_PRODUCTION = 0x03
PM_TYPE_ADDITIONAL_CONSUMPTION = 0x04
PM_TYPE_FARM = 0x05
PM_TYPE_UNUSED = 0x06
PM_TYPE_WALLBOX = 0x07
PM_TYPE_FARM_ADDITIONAL = 0x08


def getRscpTag(tag: int | str | RscpTag) -> RscpTag:
Expand Down Expand Up @@ -3776,13 +3779,22 @@ def getStrRscpError(errorcode: int | str | RscpError) -> str:
return errorcode.name


def getPowermeterType(powermetertype_hex):
"""Get powermeter type as string.
def getStrPowermeterType(powermetertype: int | str | PowermeterType) -> str:
"""
Convert a power meter type to its string name representation in PowermeterType enumeration.
Attributes:
powermeterType_hex (int): type of the powermeter as hex
Args:
powermetertype (int | str | PowermeterType): The power meter type to be converted.
- If int, it's assumed to be the power meter type value.
- If str, it's assumed to be the power meter type name.
- If PowermeterType, its name is used.
Returns:
str: String representation of the given powermeterType_hex
str: The name of the power meter type as a string.
"""
return powermeterTypes[powermetertype_hex]
if isinstance(powermetertype, int):
powermetertype = PowermeterType(powermetertype)
elif isinstance(powermetertype, str):
powermetertype = PowermeterType[powermetertype]

return powermetertype.name

0 comments on commit 5f72752

Please sign in to comment.