Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modbus hvac_* write registers #89695

Merged
merged 1 commit into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions homeassistant/components/modbus/__init__.py
Expand Up @@ -105,6 +105,7 @@
CONF_SWAP_WORD_BYTE,
CONF_TARGET_TEMP,
CONF_VERIFY,
CONF_WRITE_REGISTERS,
CONF_WRITE_TYPE,
CONF_ZERO_SUPPRESS,
DEFAULT_HUB,
Expand Down Expand Up @@ -232,6 +233,7 @@
vol.Optional(CONF_STEP, default=0.5): vol.Coerce(float),
vol.Optional(CONF_TEMPERATURE_UNIT, default=DEFAULT_TEMP_UNIT): cv.string,
vol.Optional(CONF_HVAC_ONOFF_REGISTER): cv.positive_int,
vol.Optional(CONF_WRITE_REGISTERS, default=False): cv.boolean,
vol.Optional(CONF_HVAC_MODE_REGISTER): vol.Maybe(
{
CONF_ADDRESS: cv.positive_int,
Expand All @@ -244,6 +246,7 @@
vol.Optional(CONF_HVAC_MODE_DRY): cv.positive_int,
vol.Optional(CONF_HVAC_MODE_FAN_ONLY): cv.positive_int,
},
vol.Optional(CONF_WRITE_REGISTERS, default=False): cv.boolean,
}
),
}
Expand Down
43 changes: 31 additions & 12 deletions homeassistant/components/modbus/climate.py
Expand Up @@ -45,6 +45,7 @@
CONF_MIN_TEMP,
CONF_STEP,
CONF_TARGET_TEMP,
CONF_WRITE_REGISTERS,
DataType,
)
from .modbus import ModbusHub
Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__(
self._attr_hvac_modes = cast(list[HVACMode], [])
self._attr_hvac_mode = None
self._hvac_mode_mapping: list[tuple[int, HVACMode]] = []
self._hvac_mode_write_type = mode_config[CONF_WRITE_REGISTERS]
mode_value_config = mode_config[CONF_HVAC_MODE_VALUES]

for hvac_mode_kw, hvac_mode in (
Expand All @@ -131,6 +133,7 @@ def __init__(

if CONF_HVAC_ONOFF_REGISTER in config:
self._hvac_onoff_register = config[CONF_HVAC_ONOFF_REGISTER]
self._hvac_onoff_write_type = config[CONF_WRITE_REGISTERS]
if HVACMode.OFF not in self._attr_hvac_modes:
self._attr_hvac_modes.append(HVACMode.OFF)
else:
Expand All @@ -147,23 +150,39 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
if self._hvac_onoff_register is not None:
# Turn HVAC Off by writing 0 to the On/Off register, or 1 otherwise.
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_onoff_register,
0 if hvac_mode == HVACMode.OFF else 1,
CALL_TYPE_WRITE_REGISTER,
)
if self._hvac_onoff_write_type:
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_onoff_register,
[0 if hvac_mode == HVACMode.OFF else 1],
CALL_TYPE_WRITE_REGISTERS,
)
else:
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_onoff_register,
0 if hvac_mode == HVACMode.OFF else 1,
CALL_TYPE_WRITE_REGISTER,
)

if self._hvac_mode_register is not None:
# Write a value to the mode register for the desired mode.
for value, mode in self._hvac_mode_mapping:
if mode == hvac_mode:
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_mode_register,
value,
CALL_TYPE_WRITE_REGISTER,
)
if self._hvac_mode_write_type:
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_mode_register,
[value],
CALL_TYPE_WRITE_REGISTERS,
)
else:
await self._hub.async_pymodbus_call(
self._slave,
self._hvac_mode_register,
value,
CALL_TYPE_WRITE_REGISTER,
)
break

await self.async_update()
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/modbus/const.py
Expand Up @@ -65,6 +65,7 @@
CONF_HVAC_MODE_AUTO = "state_auto"
CONF_HVAC_MODE_DRY = "state_dry"
CONF_HVAC_MODE_FAN_ONLY = "state_fan_only"
CONF_WRITE_REGISTERS = "write_registers"
CONF_VERIFY = "verify"
CONF_VERIFY_REGISTER = "verify_register"
CONF_VERIFY_STATE = "verify_state"
Expand Down