add coordinator class
This commit is contained in:
1 parent
6c6b035cf5
commit
cee71535bf
12 files changed
+284
-152
No files matched your search
@@ -49,7 +49,7 @@ class Area:
|
||||
|
||||
@property
|
||||
def id_zone(self) -> int:
|
||||
''' Get Zone Id '''
|
||||
''' Get area id '''
|
||||
return self._id
|
||||
|
||||
@property
|
||||
@@ -284,7 +284,6 @@ class Koolnova:
|
||||
|
||||
_LOGGER.debug("Retreive engines ...")
|
||||
for idx in range(1, const.NUM_OF_ENGINES + 1):
|
||||
_LOGGER.debug("Engine id: {}".format(idx))
|
||||
engine = Engine(engine_id = idx)
|
||||
ret, engine.throughput = await self._client.engine_throughput(engine_id = idx)
|
||||
ret, engine.state = await self._client.engine_state(engine_id = idx)
|
||||
@@ -356,7 +355,7 @@ class Koolnova:
|
||||
real_temp = zone_dict['real_temp'],
|
||||
order_temp = zone_dict['order_temp']
|
||||
))
|
||||
_LOGGER.debug("Zones registered: {}".format(self._areas))
|
||||
_LOGGER.debug("Areas registered: {}".format(self._areas))
|
||||
return True
|
||||
|
||||
@property
|
||||
@@ -376,7 +375,7 @@ class Koolnova:
|
||||
return ret, None
|
||||
for idx, area in enumerate(self._areas):
|
||||
if area.id_zone == zone_id:
|
||||
# update areas list value from modbus response
|
||||
# update areas list values from modbus response
|
||||
self._areas[idx].state = infos['state']
|
||||
self._areas[idx].register = infos['register']
|
||||
self._areas[idx].fan_mode = infos['fan']
|
||||
@@ -387,16 +386,17 @@ class Koolnova:
|
||||
return ret, self._areas[zone_id - 1]
|
||||
|
||||
async def update_all_areas(self) -> list:
|
||||
""" update all areas registered """
|
||||
""" update all areas registered and all engines values """
|
||||
##### Areas
|
||||
_ret, _vals = await self._client.areas_registered()
|
||||
if not _ret:
|
||||
_LOGGER.error("Error retreiving areas values")
|
||||
return None
|
||||
else:
|
||||
_LOGGER.debug("areas: {}".format(_vals))
|
||||
for k,v in _vals.items():
|
||||
for _idx, _area in enumerate(self._areas):
|
||||
if k == _area.id_zone:
|
||||
# update areas list values from modbus response
|
||||
self._areas[_idx].state = v['state']
|
||||
self._areas[_idx].register = v['register']
|
||||
self._areas[_idx].fan_mode = v['fan']
|
||||
@@ -404,7 +404,35 @@ class Koolnova:
|
||||
self._areas[_idx].real_temp = v['real_temp']
|
||||
self._areas[_idx].order_temp = v['order_temp']
|
||||
|
||||
return self._areas
|
||||
##### Engines
|
||||
for _idx in range(1, const.NUM_OF_ENGINES + 1):
|
||||
ret, self._engines[_idx - 1].throughput = await self._client.engine_throughput(engine_id = _idx)
|
||||
ret, self._engines[_idx - 1].state = await self._client.engine_state(engine_id = _idx)
|
||||
ret, self._engines[_idx - 1].order_temp = await self._client.engine_order_temp(engine_id = _idx)
|
||||
|
||||
##### Global mode
|
||||
ret, self._global_mode = await self._client.global_mode()
|
||||
if not ret:
|
||||
_LOGGER.error("Error retreiving global mode")
|
||||
self._global_mode = const.GlobalMode.COLD
|
||||
|
||||
##### Efficiency
|
||||
ret, self._efficiency = await self._client.efficiency()
|
||||
if not ret:
|
||||
_LOGGER.error("Error retreiving efficiency")
|
||||
self._efficiency = const.Efficiency.LOWER_EFF
|
||||
|
||||
##### Sys state
|
||||
ret, self._sys_state = await self._client.system_status()
|
||||
if not ret:
|
||||
_LOGGER.error("Error retreiving system status")
|
||||
self._sys_state = const.SysState.SYS_STATE_OFF
|
||||
|
||||
return {"areas": self._areas,
|
||||
"engines": self._engines,
|
||||
"glob": self._global_mode,
|
||||
"eff": self._efficiency,
|
||||
"sys": self._sys_state}
|
||||
|
||||
@property
|
||||
def engines(self) -> list:
|
||||
@@ -439,6 +467,7 @@ class Koolnova:
|
||||
raise AssertionError('Input variable must be Enum GlobalMode')
|
||||
ret = await self._client.set_global_mode(val)
|
||||
if not ret:
|
||||
_LOGGER.error("[GLOBAL] Error writing {} to modbus".format(val))
|
||||
raise UpdateValueError('Error writing to modbus updated value')
|
||||
self._global_mode = val
|
||||
|
||||
@@ -456,6 +485,7 @@ class Koolnova:
|
||||
raise AssertionError('Input variable must be Enum Efficiency')
|
||||
ret = await self._client.set_efficiency(val)
|
||||
if not ret:
|
||||
_LOGGER.error("[EFF] Error writing {} to modbus".format(val))
|
||||
raise UpdateValueError('Error writing to modbus updated value')
|
||||
self._efficiency = val
|
||||
|
||||
@@ -472,6 +502,7 @@ class Koolnova:
|
||||
raise AssertionError('Input variable must be Enum SysState')
|
||||
ret = await self._client.set_system_status(val)
|
||||
if not ret:
|
||||
_LOGGER.error("[SYS_STATE] Error writing {} to modbus".format(val))
|
||||
raise UpdateValueError('Error writing to modbus updated value')
|
||||
self._sys_state = val
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ _LOGGER = log.getLogger(__name__)
|
||||
class Operations:
|
||||
''' koolnova BMS Modbus operations class '''
|
||||
|
||||
def __init__(self, port:str, timeout:int) -> None:
|
||||
def __init__(self, port:str, timeout:int, debug:bool=False) -> None:
|
||||
''' Class constructor '''
|
||||
self._port = port
|
||||
self._timeout = timeout
|
||||
@@ -33,8 +33,8 @@ class Operations:
|
||||
stopbits=self._stopbits,
|
||||
bytesize=self._bytesize,
|
||||
timeout=self._timeout)
|
||||
|
||||
pymodbus_apply_logging_config("DEBUG")
|
||||
if debug:
|
||||
pymodbus_apply_logging_config("DEBUG")
|
||||
|
||||
def __init__(self,
|
||||
port:str="",
|
||||
@@ -43,7 +43,8 @@ class Operations:
|
||||
parity:str=const.DEFAULT_PARITY,
|
||||
stopbits:int=const.DEFAULT_STOPBITS,
|
||||
bytesize:int=const.DEFAULT_BYTESIZE,
|
||||
timeout:int=1) -> None:
|
||||
timeout:int=1,
|
||||
debug:bool=False) -> None:
|
||||
''' Class constructor '''
|
||||
self._port = port
|
||||
self._addr = addr
|
||||
@@ -58,8 +59,8 @@ class Operations:
|
||||
stopbits=self._stopbits,
|
||||
bytesize=self._bytesize,
|
||||
timeout=self._timeout)
|
||||
|
||||
pymodbus_apply_logging_config("DEBUG")
|
||||
if debug:
|
||||
pymodbus_apply_logging_config("DEBUG")
|
||||
|
||||
async def __read_register(self, reg:int) -> (int, bool):
|
||||
''' Read one holding register (code 0x03) '''
|
||||
@@ -67,7 +68,7 @@ class Operations:
|
||||
if not self._client.connected:
|
||||
raise ModbusConnexionError('Client Modbus not connected')
|
||||
try:
|
||||
_LOGGER.debug("reading holding register: {} - Addr: {}".format(hex(reg), self._addr))
|
||||
#_LOGGER.debug("reading holding register: {} - Addr: {}".format(hex(reg), self._addr))
|
||||
rr = await self._client.read_holding_registers(address=reg, count=1, slave=self._addr)
|
||||
if rr.isError():
|
||||
_LOGGER.error("reading holding register error")
|
||||
@@ -113,7 +114,7 @@ class Operations:
|
||||
if not self._client.connected:
|
||||
raise ModbusConnexionError('Client Modbus not connected')
|
||||
try:
|
||||
_LOGGER.debug("writing single register: {} - Addr: {} - Val: {}".format(hex(reg), self._addr, hex(val)))
|
||||
#_LOGGER.debug("writing single register: {} - Addr: {} - Val: {}".format(hex(reg), self._addr, hex(val)))
|
||||
rq = await self._client.write_register(address=reg, value=val, slave=self._addr)
|
||||
if rq.isError():
|
||||
_LOGGER.error("writing register error")
|
||||
@@ -174,7 +175,7 @@ class Operations:
|
||||
zone_id:int = 0,
|
||||
) -> (bool, dict):
|
||||
''' Get Zone Status from Id '''
|
||||
_LOGGER.debug("Area : {}".format(zone_id))
|
||||
#_LOGGER.debug("Area : {}".format(zone_id))
|
||||
if zone_id > const.NB_ZONE_MAX or zone_id == 0:
|
||||
raise ZoneIdError('Zone Id must be between 1 to {}'.format(const.NB_ZONE_MAX))
|
||||
zone_dict = {}
|
||||
@@ -338,7 +339,7 @@ class Operations:
|
||||
return False
|
||||
ret = await self.__write_register(reg = const.REG_START_ZONE + (4 * (zone_id - 1)) + const.REG_TEMP_ORDER, val = int(val * 2))
|
||||
if not ret:
|
||||
_LOGGER.error('Error writing zone order temperature')
|
||||
_LOGGER.error('Error writing area order temperature')
|
||||
|
||||
return ret
|
||||
|
||||
@@ -389,13 +390,13 @@ class Operations:
|
||||
""" set area state """
|
||||
register:const.ZoneRegister = const.ZoneRegister.REGISTER_OFF
|
||||
if id_zone > const.NB_ZONE_MAX or id_zone == 0:
|
||||
raise ZoneIdError('Zone Id must be between 1 to 16')
|
||||
raise ZoneIdError('Area Id must be between 1 to 16')
|
||||
# retreive values to combine the new state with register read
|
||||
ret, register, _ = await self.area_state_and_register(id_zone = id_zone)
|
||||
if not ret:
|
||||
_LOGGER.error("Error reading state and register mode")
|
||||
return ret
|
||||
_LOGGER.debug("register & state: {}".format(hex((int(register) << 1) | (int(val) & 0b01))))
|
||||
#_LOGGER.debug("register & state: {}".format(hex((int(register) << 1) | (int(val) & 0b01))))
|
||||
ret = await self.__write_register(reg = const.REG_START_ZONE + (4 * (id_zone - 1)) + const.REG_LOCK_ZONE,
|
||||
val = int(int(register) << 1) | (int(val) & 0b01))
|
||||
if not ret:
|
||||
@@ -416,7 +417,7 @@ class Operations:
|
||||
if not ret:
|
||||
_LOGGER.error("Error reading fan and clim mode")
|
||||
return ret
|
||||
_LOGGER.debug("Fan & Clim: {}".format(hex((int(fan) << 4) | (int(val) & 0x0F))))
|
||||
#_LOGGER.debug("Fan & Clim: {}".format(hex((int(fan) << 4) | (int(val) & 0x0F))))
|
||||
ret = await self.__write_register(reg = const.REG_START_ZONE + (4 * (id_zone - 1)) + const.REG_STATE_AND_FLOW,
|
||||
val = int(int(fan) << 4) | (int(val) & 0x0F))
|
||||
if not ret:
|
||||
@@ -437,7 +438,7 @@ class Operations:
|
||||
if not ret:
|
||||
_LOGGER.error("Error reading fan and clim mode")
|
||||
return ret
|
||||
_LOGGER.debug("Fan & Clim: {}".format(hex((int(val) << 4) | (int(clim) & 0x0F))))
|
||||
#_LOGGER.debug("Fan & Clim: {}".format(hex((int(val) << 4) | (int(clim) & 0x0F))))
|
||||
ret = await self.__write_register(reg = const.REG_START_ZONE + (4 * (id_zone - 1)) + const.REG_STATE_AND_FLOW,
|
||||
val = int(int(val) << 4) | (int(clim) & 0x0F))
|
||||
if not ret:
|
||||
|
||||
Reference in new issue
Block a user