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

Modbus: Add support for Holding Registers to Binary Sensor #80460

Merged
merged 7 commits into from Oct 18, 2022
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
2 changes: 1 addition & 1 deletion homeassistant/components/modbus/__init__.py
Expand Up @@ -268,7 +268,7 @@
{
vol.Optional(CONF_DEVICE_CLASS): BINARY_SENSOR_DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_INPUT_TYPE, default=CALL_TYPE_COIL): vol.In(
[CALL_TYPE_COIL, CALL_TYPE_DISCRETE]
[CALL_TYPE_COIL, CALL_TYPE_DISCRETE, CALL_TYPE_REGISTER_HOLDING]
),
vol.Optional(CONF_SLAVE_COUNT, default=0): cv.positive_int,
}
Expand Down
7 changes: 5 additions & 2 deletions homeassistant/components/modbus/binary_sensor.py
Expand Up @@ -23,7 +23,7 @@

from . import get_hub
from .base_platform import BasePlatform
from .const import CONF_SLAVE_COUNT
from .const import CALL_TYPE_COIL, CALL_TYPE_DISCRETE, CONF_SLAVE_COUNT
from .modbus import ModbusHub

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -109,9 +109,12 @@ async def async_update(self, now: datetime | None = None) -> None:
self._result = None
else:
self._lazy_errors = self._lazy_error_count
self._attr_is_on = result.bits[0] & 1
self._attr_available = True
self._result = result
if self._input_type in (CALL_TYPE_COIL, CALL_TYPE_DISCRETE):
self._attr_is_on = bool(result.bits[0] & 1)
else:
self._attr_is_on = bool(result.registers[0] & 1)

self.async_write_ha_state()
if self._coordinator:
Expand Down
10 changes: 10 additions & 0 deletions tests/components/modbus/test_binary_sensor.py
Expand Up @@ -5,6 +5,7 @@
from homeassistant.components.modbus.const import (
CALL_TYPE_COIL,
CALL_TYPE_DISCRETE,
CALL_TYPE_REGISTER_HOLDING,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_SLAVE_COUNT,
Expand Down Expand Up @@ -81,6 +82,15 @@ async def test_config_binary_sensor(hass, mock_modbus):
},
],
},
{
CONF_BINARY_SENSORS: [
{
CONF_NAME: TEST_ENTITY_NAME,
CONF_ADDRESS: 51,
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
},
],
},
],
)
@pytest.mark.parametrize(
Expand Down