|
5 | 5 |
|
6 | 6 | from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
7 | 7 |
|
8 | | -from pymodbus import ModbusException |
9 | 8 | import pymodbus.client as ModbusClient |
10 | 9 | from pymodbus.exceptions import ConnectionException, ModbusException |
11 | 10 | from pymodbus.payload import BinaryPayloadDecoder, Endian |
|
16 | 15 |
|
17 | 16 |
|
18 | 17 | class SolvisModbusCoordinator(DataUpdateCoordinator): |
19 | | - """My custom coordinator.""" |
| 18 | + """Coordinates data updates from a Solvis device via Modbus.""" |
20 | 19 |
|
21 | 20 | def __init__( |
22 | 21 | self, |
23 | 22 | hass, |
24 | | - conf_host, |
25 | | - conf_port, |
26 | | - conf_option_1: bool, |
27 | | - conf_option_2: bool, |
28 | | - conf_option_3: bool, |
29 | | - conf_option_4: bool, |
| 23 | + host: str, |
| 24 | + port: int, |
| 25 | + option_hkr2: bool, |
| 26 | + option_hkr3: bool, |
| 27 | + option_solar: bool, |
| 28 | + option_heatpump: bool, |
30 | 29 | ): |
31 | | - """Initialize my coordinator.""" |
| 30 | + """Initializes the Solvis Modbus data coordinator.""" |
32 | 31 | super().__init__( |
33 | 32 | hass, |
34 | 33 | _LOGGER, |
35 | | - # Name of the data. For logging purposes. |
36 | 34 | name=DOMAIN, |
37 | | - # Polling interval. Will only be polled if there are subscribers. |
38 | 35 | update_interval=timedelta(seconds=30), |
39 | 36 | ) |
40 | | - self.hass = hass |
41 | | - self.CONF_OPTION_4 = conf_option_4 |
42 | | - self.CONF_OPTION_3 = conf_option_3 |
43 | | - self.CONF_OPTION_2 = conf_option_2 |
44 | | - self.CONF_OPTION_1 = conf_option_1 |
45 | | - self.logger.debug("Creating client") |
46 | | - self.modbus = ModbusClient.AsyncModbusTcpClient(host=conf_host, port=conf_port) |
| 37 | + self.host = host |
| 38 | + self.port = port |
| 39 | + self.option_hkr2 = option_hkr2 |
| 40 | + self.option_hkr3 = option_hkr3 |
| 41 | + self.option_solar = option_solar |
| 42 | + self.option_heatpump = option_heatpump |
47 | 43 |
|
48 | | - async def _async_update_data(self): |
49 | | - """Fetch data from API endpoint. |
| 44 | + _LOGGER.debug("Creating Modbus client") |
| 45 | + self.modbus = ModbusClient.AsyncModbusTcpClient(host=host, port=port) |
50 | 46 |
|
51 | | - This is the place to pre-process the data to lookup tables |
52 | | - so entities can quickly look up their data. |
53 | | - """ |
54 | | - self.logger.debug("Polling data") |
| 47 | + async def _async_update_data(self): |
| 48 | + """Fetches and processes data from the Solvis device.""" |
55 | 49 |
|
56 | | - parsed_data: dict = {} |
57 | | - entity_registry = self.hass.data["entity_registry"] |
| 50 | + _LOGGER.debug("Polling data") |
| 51 | + parsed_data = {} |
58 | 52 |
|
59 | 53 | try: |
60 | 54 | await self.modbus.connect() |
61 | | - except ConnectionException: |
62 | | - self.logger.warning("Couldn't connect to device") |
63 | | - if self.modbus.connected: |
| 55 | + _LOGGER.debug( |
| 56 | + "Connected to Modbus for Solvis" |
| 57 | + ) # Moved here for better context |
| 58 | + |
64 | 59 | for register in REGISTERS: |
65 | | - if not self.CONF_OPTION_1 and register.conf_option == 1: |
| 60 | + if not self.option_hkr2 and register.conf_option == 1: |
66 | 61 | continue |
67 | | - if not self.CONF_OPTION_2 and register.conf_option == 2: |
| 62 | + if not self.option_hkr3 and register.conf_option == 2: |
68 | 63 | continue |
69 | | - if not self.CONF_OPTION_3 and register.conf_option == 3: |
| 64 | + if not self.option_solar and register.conf_option == 3: |
70 | 65 | continue |
71 | | - if not self.CONF_OPTION_4 and register.conf_option == 4: |
| 66 | + if not self.option_heatpump and register.conf_option == 4: |
72 | 67 | continue |
73 | 68 |
|
74 | 69 | entity_id = f"{DOMAIN}.{register.name}" |
75 | | - entity_entry = entity_registry.async_get(entity_id) |
| 70 | + entity_entry = self.hass.data["entity_registry"].async_get(entity_id) |
76 | 71 | if entity_entry and entity_entry.disabled: |
77 | | - self.logger.debug(f"Skipping disabled entity: {entity_id}") |
| 72 | + _LOGGER.debug(f"Skipping disabled entity: {entity_id}") |
78 | 73 | continue |
79 | 74 |
|
80 | | - self.logger.debug("Connected to Modbus for Solvis") |
81 | 75 | try: |
82 | 76 | if register.register == 1: |
83 | 77 | result = await self.modbus.read_input_registers( |
84 | 78 | register.address, 1, 1 |
85 | 79 | ) |
86 | | - elif register.register == 2: |
| 80 | + else: |
87 | 81 | result = await self.modbus.read_holding_registers( |
88 | 82 | register.address, 1, 1 |
89 | 83 | ) |
90 | | - except ModbusException as error: |
91 | | - self.logger.error(error) |
92 | | - else: |
93 | | - d = BinaryPayloadDecoder.fromRegisters( |
| 84 | + |
| 85 | + decoder = BinaryPayloadDecoder.fromRegisters( |
94 | 86 | result.registers, byteorder=Endian.BIG |
95 | 87 | ) |
96 | | - parsed_data[register.name] = round( |
97 | | - d.decode_16bit_int() * register.multiplier, 2 |
| 88 | + value = round(decoder.decode_16bit_int() * register.multiplier, 2) |
| 89 | + parsed_data[register.name] = ( |
| 90 | + abs(value) if register.absolute_value else value |
98 | 91 | ) |
99 | | - if register.absolute_value: |
100 | | - parsed_data[register.name] = abs(parsed_data[register.name]) |
101 | | - self.modbus.close() |
102 | 92 |
|
103 | | - # Pass data back to sensors |
| 93 | + except ModbusException as error: |
| 94 | + _LOGGER.error( |
| 95 | + f"Modbus error reading register {register.name}: {error}" |
| 96 | + ) |
| 97 | + |
| 98 | + except ConnectionException: |
| 99 | + _LOGGER.warning("Couldn't connect to Solvis device") |
| 100 | + finally: |
| 101 | + self.modbus.close() |
| 102 | + |
104 | 103 | return parsed_data |
0 commit comments