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

Fix slave id equal to 0 #136263

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion homeassistant/components/modbus/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from homeassistant.helpers.restore_state import RestoreEntity

from .const import (
ATTR_SLAVE,
CALL_TYPE_COIL,
CALL_TYPE_DISCRETE,
CALL_TYPE_REGISTER_HOLDING,
Expand Down Expand Up @@ -79,14 +80,19 @@ def __init__(
"""Initialize the Modbus binary sensor."""

self._hub = hub
self._slave = entry.get(CONF_SLAVE) or entry.get(CONF_DEVICE_ADDRESS, 1)
self._slave = (
entry.get(CONF_SLAVE)
if entry.get(CONF_SLAVE) is not None
else entry.get(CONF_DEVICE_ADDRESS, 1)
)
crug80 marked this conversation as resolved.
Show resolved Hide resolved
self._address = int(entry[CONF_ADDRESS])
self._input_type = entry[CONF_INPUT_TYPE]
self._value: str | None = None
self._scan_interval = int(entry[CONF_SCAN_INTERVAL])
self._call_active = False
self._cancel_timer: Callable[[], None] | None = None
self._cancel_call: Callable[[], None] | None = None
self._attr_extra_state_attributes = {ATTR_SLAVE: self._slave}
crug80 marked this conversation as resolved.
Show resolved Hide resolved

self._attr_unique_id = entry.get(CONF_UNIQUE_ID)
self._attr_name = entry[CONF_NAME]
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ async def low_level_pb_call(
self, slave: int | None, address: int, value: int | list[int], use_call: str
) -> ModbusPDU | None:
"""Call sync. pymodbus."""
kwargs: dict[str, Any] = {"slave": slave} if slave is not None else {"slave": 1}
kwargs: dict[str, Any] = (
{ATTR_SLAVE: slave} if slave is not None else {ATTR_SLAVE: 1}
)
entry = self._pb_request[use_call]
kwargs[entry.value_attr_name] = value
try:
Expand Down
38 changes: 38 additions & 0 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,3 +1274,41 @@ async def test_no_entities(hass: HomeAssistant) -> None:
]
}
assert await async_setup_component(hass, DOMAIN, config) is False


async def test_check_defaultslave(
hass: HomeAssistant,
) -> None:
"""Test default slave."""
config = {
DOMAIN: [
{
CONF_TYPE: TCP,
CONF_HOST: TEST_MODBUS_HOST,
CONF_PORT: TEST_PORT_TCP,
CONF_NAME: TEST_MODBUS_NAME,
CONF_SENSORS: [
{
CONF_NAME: "dummy",
CONF_ADDRESS: 1234,
},
{
CONF_NAME: "dummy_2",
CONF_ADDRESS: 1234,
CONF_SLAVE: 0,
},
{
CONF_NAME: "dummy_3",
CONF_ADDRESS: 1234,
CONF_DEVICE_ADDRESS: 6,
},
],
}
]
}
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
assert DOMAIN in hass.config.components
assert hass.states.get("sensor.dummy").attributes.get(ATTR_SLAVE) == 1
assert hass.states.get("sensor.dummy_2").attributes.get(ATTR_SLAVE) == 0
assert hass.states.get("sensor.dummy_3").attributes.get(ATTR_SLAVE) == 6
Loading