Skip to content

Commit

Permalink
Property value in the EPC_FUNCTIONS constant (#68)
Browse files Browse the repository at this point in the history
* Property value in the EPC_FUNCTIONS constant

Rel. #67

* Adjustment of status wording
  • Loading branch information
nao-pon authored Feb 3, 2024
1 parent e5493c6 commit 9d6b794
Show file tree
Hide file tree
Showing 25 changed files with 1,743 additions and 1,288 deletions.
98 changes: 56 additions & 42 deletions pychonet/CeilingFan.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from deprecated import deprecated
from pychonet.EchonetInstance import EchonetInstance
from pychonet.lib.epc_functions import DICT_30_TRUE_FALSE
from pychonet.lib.epc_functions import _int, _swap_dict

ENL_FANSPEED_PERCENT = 0xF0
ENL_FAN_DIRECTION = 0xF1
ENL_FAN_OSCILLATION = 0xF2
ENL_BUZZER = 0xFC
ENL_FAN_POWER = 0x80

FAN_DIRECTION = {
"forward": 0x41,
"reverse": 0x42
}
DICT_FAN_DIRECTION = {0x41: "forward", 0x42: "reverse"}

FAN_DIRECTION = _swap_dict(DICT_FAN_DIRECTION)

FAN_OSCILLATION = _swap_dict(DICT_30_TRUE_FALSE)

FAN_OSCILLATION = {
True: 0x30,
False: 0x31
}

# ----- Ceiling Fan Class -------
# Fan speed in percentage
Expand All @@ -23,32 +23,34 @@ def _013AF0(edt):
fan_speed_percentage = int((op_mode - 0x30) * 0x0A)
return fan_speed_percentage


# Fan Direction
@deprecated(reason="Scheduled for removal.")
def _013AF1(edt):
op_mode = int.from_bytes(edt, "big")
values = {0x41: "forward", 0x42: "reverse"}
return values.get(op_mode, "invalid_setting")
return _int(edt, DICT_FAN_DIRECTION)


# Fan Fluctuation
@deprecated(reason="Scheduled for removal.")
def _013AF2(edt):
op_mode = int.from_bytes(edt, "big")
values = {0x30: True, 0x31: False}
return values.get(op_mode, "invalid_setting")
return _int(edt, DICT_30_TRUE_FALSE)


# Fan Buzzer
@deprecated(reason="Scheduled for removal.")
def _013AFC(edt):
op_mode = int.from_bytes(edt, "big")
values = {0x30: True, 0x31: False}
return values.get(op_mode, "invalid_setting")
return _int(edt, DICT_30_TRUE_FALSE)


"""Class for Celing Fan Objects"""
class CeilingFan(EchonetInstance):


class CeilingFan(EchonetInstance):
EPC_FUNCTIONS = {
0xF0: _013AF0,
0xF1: _013AF1,
0xF2: _013AF2,
0xFC: _013AFC
0xF1: [_int, DICT_FAN_DIRECTION],
0xF2: [_int, DICT_30_TRUE_FALSE],
0xFC: [_int, DICT_30_TRUE_FALSE],
}

def __init__(self, host, api_connector=None, instance=0x1):
Expand All @@ -64,12 +66,16 @@ def __init__(self, host, api_connector=None, instance=0x1):
param fans_speed: An int representing the fan speed.
"""

def setFanSpeedPercent(self, fan_speed_percent):
return self.setMessages(
async def setFanSpeedPercent(self, fan_speed_percent):
return await self.setMessages(
[
{"EPC":ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_FANSPEED_PERCENT, "PDC": 0x01, "EDT": round(fan_speed_percent/10) + 0x30}
{"EPC": ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC": ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{
"EPC": ENL_FANSPEED_PERCENT,
"PDC": 0x01,
"EDT": round(fan_speed_percent / 10) + 0x30,
},
]
)

Expand All @@ -80,8 +86,8 @@ def setFanSpeedPercent(self, fan_speed_percent):
return: A string representing the fan speed
"""

def getFanSpeedPercent(self): # 0xF0
return self.getMessage(ENL_FANSPEED_PERCENT)
async def getFanSpeedPercent(self): # 0xF0
return await self.getMessage(ENL_FANSPEED_PERCENT)

"""
setFanDirection set the fan direction
Expand All @@ -90,12 +96,16 @@ def getFanSpeedPercent(self): # 0xF0
e.g: 'forward', 'reverse'.
"""

def setFanDirection(self, fan_direction):
return self.setMessages(
async def setFanDirection(self, fan_direction):
return await self.setMessages(
[
{"EPC":ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_FAN_DIRECTION, "PDC": 0x01, "EDT": FAN_DIRECTION[fan_direction]}
{"EPC": ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC": ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{
"EPC": ENL_FAN_DIRECTION,
"PDC": 0x01,
"EDT": FAN_DIRECTION[fan_direction],
},
]
)

Expand All @@ -106,8 +116,8 @@ def setFanDirection(self, fan_direction):
return: A string representing the fan direction
"""

def getFanDirection(self): # 0xF1
return self.getMessage(ENL_FAN_DIRECTION)
async def getFanDirection(self): # 0xF1
return await self.getMessage(ENL_FAN_DIRECTION)

"""
setFanOscillation set the fan oscillation
Expand All @@ -116,12 +126,16 @@ def getFanDirection(self): # 0xF1
e.g: 'True', 'False'.
"""

def setFanOscillation(self, fan_oscillation):
return self.setMessages(
async def setFanOscillation(self, fan_oscillation):
return await self.setMessages(
[
{"EPC":ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{"EPC":ENL_FAN_OSCILLATION, "PDC": 0x01, "EDT": FAN_OSCILLATION[fan_oscillation]}
{"EPC": ENL_FAN_POWER, "PDC": 0x01, "EDT": 0x30},
{"EPC": ENL_BUZZER, "PDC": 0x01, "EDT": 0x30},
{
"EPC": ENL_FAN_OSCILLATION,
"PDC": 0x01,
"EDT": FAN_OSCILLATION[fan_oscillation],
},
]
)

Expand All @@ -132,5 +146,5 @@ def setFanOscillation(self, fan_oscillation):
return: A boolean representing the fan oscillation status
"""

def getFanOscillation(self): # 0xF2
return self.getMessage(ENL_FAN_OSCILLATION)
async def getFanOscillation(self): # 0xF2
return await self.getMessage(ENL_FAN_OSCILLATION)
33 changes: 25 additions & 8 deletions pychonet/EchonetInstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,32 @@ async def update(self, attributes=None, no_request=False):
elif epc in list(
self.EPC_FUNCTIONS.keys()
): # check the class-specific EPC function table.
returned_json_data.update(
{
epc: self.EPC_FUNCTIONS[epc](
self._api._state[self._host]["instances"][self._eojgc][
self._eojcc
][self._eojci][epc]
raw_data = self._api._state[self._host]["instances"][self._eojgc][
self._eojcc
][self._eojci][epc]
if type(self.EPC_FUNCTIONS[epc]) == list:
if list(self.EPC_FUNCTIONS[epc]) == 3:
data = self.EPC_FUNCTIONS[epc][0](
raw_data,
self.EPC_FUNCTIONS[epc][1],
self.EPC_FUNCTIONS[epc][2],
)
}
)
else:
data = self.EPC_FUNCTIONS[epc][0](
raw_data, self.EPC_FUNCTIONS[epc][1]
)
else:
data = self.EPC_FUNCTIONS[epc](raw_data)
returned_json_data.update({epc: data})
# returned_json_data.update(
# {
# epc: self.EPC_FUNCTIONS[epc](
# self._api._state[self._host]["instances"][self._eojgc][
# self._eojcc
# ][self._eojci][epc]
# )
# }
# )
continue
elif epc in list(
EPC_CODE[self._eojgc][self._eojcc].keys()
Expand Down
64 changes: 39 additions & 25 deletions pychonet/ElectricBlind.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
from deprecated import deprecated
from pychonet.EchonetInstance import EchonetInstance
from pychonet.lib.epc_functions import (
DATA_STATE_CLOSE,
DATA_STATE_OPEN,
DATA_STATE_STOP,
_int,
)

ENL_OPENSTATE = 0xE0

DICT_41_OPEN_CLOSE_STOP = {
0x41: DATA_STATE_OPEN,
0x42: DATA_STATE_CLOSE,
0x43: DATA_STATE_STOP,
}


@deprecated(reason="Scheduled for removal.")
def _0260EO(edt):
op_mode = int.from_bytes(edt, "big")
values = {0x41: "open", 0x42: "close", 0x43: "stop"}
return values.get(op_mode, "invalid_setting")
return _int(edt, DICT_41_OPEN_CLOSE_STOP)


# TODO - complete class definitions
# 0x80: 'Operation status',
# 0x89: 'Fault description (Recoverable faults)',
# 0x90: 'Timer operation setting',
# 0xC2: 'Wind detection status',
# 0xC3: 'Sunlight detection status',
# 0xD0: 'Opening (extension) speed setting',
# 0xD1: 'Closing (retraction) speed setting',
# 0xD2: 'Operation time',
# 0xD4: 'Automatic operation setting',
# 0xE1: 'Degree-of-opening level',
# 0xE2: 'Shade angle setting ',
# 0xE3: 'Open/close (extension/retraction) speed',
# 0xE5: 'Electric lock setting',
# 0xE8: 'Remote operation setting status',
# 0xE9: 'Selective opening (extension) operation setting',
# 0xEA: 'Open/closed (extended/retracted) status',
# 0xEE: 'One-time opening (extension) speed setting',
# 0xEF: 'One-time closing (retraction) speed setting'
# 0x80: 'Operation status',
# 0x89: 'Fault description (Recoverable faults)',
# 0x90: 'Timer operation setting',
# 0xC2: 'Wind detection status',
# 0xC3: 'Sunlight detection status',
# 0xD0: 'Opening (extension) speed setting',
# 0xD1: 'Closing (retraction) speed setting',
# 0xD2: 'Operation time',
# 0xD4: 'Automatic operation setting',
# 0xE1: 'Degree-of-opening level',
# 0xE2: 'Shade angle setting ',
# 0xE3: 'Open/close (extension/retraction) speed',
# 0xE5: 'Electric lock setting',
# 0xE8: 'Remote operation setting status',
# 0xE9: 'Selective opening (extension) operation setting',
# 0xEA: 'Open/closed (extended/retracted) status',
# 0xEE: 'One-time opening (extension) speed setting',
# 0xEF: 'One-time closing (retraction) speed setting'


"""Class for Electric Blind/Shade Objects"""


class ElectricBlind(EchonetInstance):
EOJGC = 0x02 # Housing/facility-related device group
EOJCC = 0x60 # Electrically operated blind/shade

EPC_FUNCTIONS = {0xE0: _0260EO}
EPC_FUNCTIONS = {
0xE0: [_int, DICT_41_OPEN_CLOSE_STOP],
}

def __init__(self, host, api_connector=None, instance=0x1):
self._eojgc = 0x02 # Housing/facility-related device group
self._eojcc = 0x60 # Electrically operated blind/shade
EchonetInstance.__init__(
self, host, self._eojgc, self._eojcc, instance, api_connector
self, host, self.EOJGC, self.EOJCC, instance, api_connector
)

"""
Expand Down
9 changes: 4 additions & 5 deletions pychonet/ElectricEnergyMeter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from pychonet.EchonetInstance import EchonetInstance
from pychonet.lib.epc_functions import _int, _signed_int


def _0280E2(edt):
# return array x 48 unsigned long big-endian
op_mode = int.from_bytes(edt, "big")
values = {0x01: 0.1, 0x02: 0.01}
return values.get(op_mode, "invalid_setting")
# return array x 48 unsigned long big-endian
return _int(edt, {0x01: 0.1, 0x02: 0.01})


class ElectricEnergyMeter(EchonetInstance):
EPC_FUNCTIONS = {
0xE0: _int, # 0xE0: "Cumulative amounts of electric energy measurement value",
0xE2: _0280E2 # 0xE2: "Cumulative amounts of electric energy unit",
0xE2: _0280E2, # 0xE2: "Cumulative amounts of electric energy unit",
}

def __init__(self, host, api_connector=None, instance=0x1):
Expand Down
Loading

0 comments on commit 9d6b794

Please sign in to comment.