-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play around with a Heatpump object and pulling all
at once
- Loading branch information
Showing
3 changed files
with
83 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import warnings | ||
|
||
from pymodbus.client import ModbusTcpClient as ModbusClient | ||
|
||
from homeassistant.const import CONF_HOST, CONF_PORT | ||
|
||
from .const import TYPES | ||
from .hpconst import MODBUS_SYS_ITEMS | ||
|
||
|
||
class HeatPump: | ||
_ip = None | ||
_port = None | ||
_ModbusClient = None | ||
|
||
def __init__(self, config_entry): | ||
self._ip = config_entry.data[CONF_HOST] | ||
self._port = config_entry.data[CONF_PORT] | ||
self._ModbusClient = None | ||
|
||
def connect(self): | ||
# try: | ||
self._ModbusClient = ModbusClient(host=self._ip, port=self._port) | ||
return self._ModbusClient.connected # noqa: TRY300 | ||
|
||
# except: # noqa: E722 | ||
# return None | ||
|
||
def getValue(self, ModbusItem): | ||
try: | ||
if not self._ModbusClient.connected: | ||
self.connect() | ||
match ModbusItem.type: | ||
case TYPES.SENSOR | TYPES.SENSOR_CALC: | ||
# Sensor entities are read-only | ||
ModbusItem._value = self._ModbusClient.read_input_registers( | ||
ModbusItem.address, slave=1 | ||
).registers[0] | ||
case TYPES.SELECT | TYPES.NUMBER | TYPES.NUMBER_RO: | ||
ModbusItem._value = self._ModbusClient.read_holding_registers( | ||
ModbusItem.address, slave=1 | ||
).registers[0] | ||
|
||
except: # noqa: E722 | ||
warnings.warn("Getting value of " & ModbusItem.name & " failed") | ||
return None | ||
|
||
def setValue(self, ModbusItem, value) -> None: | ||
try: | ||
match ModbusItem.type: | ||
case TYPES.SENSOR | TYPES.NUMBER_RO | TYPES.SENSOR_CALC: | ||
# Sensor entities are read-only | ||
return | ||
case _: | ||
self.connect() | ||
self._ModbusClient.write_register( | ||
ModbusItem.address, int(value), slave=1 | ||
) | ||
except: # noqua: E722 | ||
return None | ||
|
||
def poll(self): | ||
for item in MODBUS_SYS_ITEMS: | ||
# warnings.warn(str(item.address)) | ||
self.getValue(item) | ||
warnings.warn(str(item._value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters