Skip to content

Commit e6b4a04

Browse files
committed
Minor refactor to modbus client
1 parent f0e5a79 commit e6b4a04

6 files changed

Lines changed: 212 additions & 215 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Medole Dehumidifier Integration for Home Assistant
1+
# Medole Dehumidifier (米多力除溼機) Integration for Home Assistant
22

3-
This is a custom component for Home Assistant that integrates Medole Dehumidifier devices via Modbus (serial or TCP).
3+
This is a custom component for Home Assistant that integrates Medole Dehumidifier devices via Modbus (serial or TCP). You need to connect a RS485 to Ethernet/Wi-Fi converter to the RS485 port of the Medole Dehumidifier.
44

55
## Features
66

custom_components/medole/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from homeassistant.core import HomeAssistant
88

99
from .const import CONF_SLAVE_ID, DOMAIN
10-
from .modbus import create_modbus_client
10+
from .modbus import MedoleModbusClient
1111

1212
_LOGGER = logging.getLogger(__name__)
1313

@@ -19,13 +19,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1919
config = entry.data
2020

2121
# Create the Modbus client once
22-
client = create_modbus_client(config)
22+
slave_id = config[CONF_SLAVE_ID]
23+
modbus_client = MedoleModbusClient(hass, config, slave_id)
2324

2425
hass.data.setdefault(DOMAIN, {})
2526
hass.data[DOMAIN][entry.entry_id] = {
26-
"client": client,
27+
"client": modbus_client,
2728
"config": config,
28-
"slave_id": config[CONF_SLAVE_ID],
29+
"slave_id": slave_id,
2930
}
3031

3132
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

custom_components/medole/humidifier.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
STATUS_COMPRESSOR_ON,
3232
STATUS_FAN_ON,
3333
)
34-
from .modbus import async_read_register, async_write_register
34+
35+
# No need to import modbus functions as we'll use the client methods directly
3536

3637
_LOGGER = logging.getLogger(__name__)
3738

@@ -54,12 +55,11 @@ async def async_setup_entry(
5455
data = hass.data[DOMAIN][config_entry.entry_id]
5556
config = data["config"]
5657
client = data["client"]
57-
slave_id = data["slave_id"]
5858

5959
name = config[CONF_NAME]
6060

6161
async_add_entities(
62-
[MedoleDehumidifierHumidifier(hass, name, client, slave_id)],
62+
[MedoleDehumidifierHumidifier(hass, name, client)],
6363
True,
6464
)
6565

@@ -75,11 +75,10 @@ class MedoleDehumidifierHumidifier(HumidifierEntity):
7575
_attr_min_humidity = MIN_HUMIDITY
7676
_attr_max_humidity = MAX_HUMIDITY
7777

78-
def __init__(self, hass, name, client, slave_id):
78+
def __init__(self, hass, name, client):
7979
"""Initialize the humidifier device."""
8080
self.hass = hass
8181
self._client = client
82-
self._slave_id = slave_id
8382
self._attr_unique_id = f"{name}_humidifier"
8483
self._attr_device_info = {
8584
"identifiers": {(DOMAIN, self._attr_unique_id)},
@@ -118,9 +117,7 @@ def max_humidity(self):
118117
async def async_update(self) -> None:
119118
"""Update the state of the humidifier device."""
120119
# Get power status
121-
power_result = await async_read_register(
122-
self.hass, self._client, REG_POWER, self._slave_id
123-
)
120+
power_result = await self._client.async_read_register(REG_POWER)
124121
if power_result:
125122
power_status = power_result.registers[0]
126123
self._attr_is_on = power_status == 1
@@ -129,8 +126,8 @@ async def async_update(self) -> None:
129126
return
130127

131128
# Get the operation status register
132-
status_result = await async_read_register(
133-
self.hass, self._client, REG_OPERATION_STATUS, self._slave_id
129+
status_result = await self._client.async_read_register(
130+
REG_OPERATION_STATUS
134131
)
135132

136133
if status_result:
@@ -151,8 +148,8 @@ async def async_update(self) -> None:
151148
_LOGGER.error("Failed to read operation status")
152149

153150
# Get the humidity setpoint
154-
setpoint_result = await async_read_register(
155-
self.hass, self._client, REG_HUMIDITY_SETPOINT, self._slave_id
151+
setpoint_result = await self._client.async_read_register(
152+
REG_HUMIDITY_SETPOINT
156153
)
157154

158155
if setpoint_result:
@@ -166,9 +163,7 @@ async def async_update(self) -> None:
166163
_LOGGER.error("Failed to read humidity setpoint")
167164

168165
# Get the fan speed
169-
fan_speed_result = await async_read_register(
170-
self.hass, self._client, REG_FAN_SPEED, self._slave_id
171-
)
166+
fan_speed_result = await self._client.async_read_register(REG_FAN_SPEED)
172167

173168
if fan_speed_result:
174169
fan_speed = fan_speed_result.registers[0]
@@ -177,9 +172,7 @@ async def async_update(self) -> None:
177172
_LOGGER.error("Failed to read fan speed")
178173

179174
# Get the current humidity
180-
humidity_result = await async_read_register(
181-
self.hass, self._client, REG_HUMIDITY_1, self._slave_id
182-
)
175+
humidity_result = await self._client.async_read_register(REG_HUMIDITY_1)
183176

184177
if humidity_result:
185178
self._attr_current_humidity = humidity_result.registers[0]
@@ -190,8 +183,8 @@ async def async_set_mode(self, mode: str) -> None:
190183
"""Set new mode."""
191184
fan_speed = REVERSE_MODES.get(mode, FAN_SPEED_MEDIUM)
192185

193-
success = await async_write_register(
194-
self.hass, self._client, REG_FAN_SPEED, fan_speed, self._slave_id
186+
success = await self._client.async_write_register(
187+
REG_FAN_SPEED, fan_speed
195188
)
196189

197190
if success:
@@ -204,12 +197,8 @@ async def async_set_humidity(self, humidity: int) -> None:
204197
# Ensure humidity is within valid range
205198
humidity = max(MIN_HUMIDITY, min(MAX_HUMIDITY, humidity))
206199

207-
success = await async_write_register(
208-
self.hass,
209-
self._client,
210-
REG_HUMIDITY_SETPOINT,
211-
humidity,
212-
self._slave_id,
200+
success = await self._client.async_write_register(
201+
REG_HUMIDITY_SETPOINT, humidity
213202
)
214203

215204
if success:
@@ -220,32 +209,24 @@ async def async_set_humidity(self, humidity: int) -> None:
220209
async def async_turn_on(self, **kwargs) -> None:
221210
"""Turn the device on."""
222211
# Set the power on
223-
result = await async_write_register(
224-
self.hass, self._client, REG_POWER, 1, self._slave_id
225-
)
212+
result = await self._client.async_write_register(REG_POWER, 1)
226213
if not result:
227214
_LOGGER.error("Failed to turn on device")
228215

229216
# Set dehumidify mode on
230-
result = await async_write_register(
231-
self.hass, self._client, REG_DEHUMIDIFY_MODE, 1, self._slave_id
232-
)
217+
result = await self._client.async_write_register(REG_DEHUMIDIFY_MODE, 1)
233218
if not result:
234219
_LOGGER.error("Failed to turn on dehumidify mode")
235220

236221
# Set purify mode off
237-
result = await async_write_register(
238-
self.hass, self._client, REG_PURIFY_MODE, 0, self._slave_id
239-
)
222+
result = await self._client.async_write_register(REG_PURIFY_MODE, 0)
240223

241224
self._attr_is_on = True
242225

243226
async def async_turn_off(self, **kwargs) -> None:
244227
"""Turn the device off."""
245228
# Set power off
246-
success = await async_write_register(
247-
self.hass, self._client, REG_POWER, 0, self._slave_id
248-
)
229+
success = await self._client.async_write_register(REG_POWER, 0)
249230

250231
if success:
251232
self._attr_is_on = False

0 commit comments

Comments
 (0)