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 4 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
11 changes: 10 additions & 1 deletion homeassistant/components/modbus/binary_sensor.py
Expand Up @@ -21,6 +21,8 @@
DataUpdateCoordinator,
)

from .const import CALL_TYPE_COIL, CALL_TYPE_DISCRETE

from . import get_hub
from .base_platform import BasePlatform
from .const import CONF_SLAVE_COUNT
Expand Down Expand Up @@ -109,9 +111,16 @@ 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:
value = int(result.registers[0])
Nippey marked this conversation as resolved.
Show resolved Hide resolved
if value > 0:
self._attr_is_on = True
else:
self._attr_is_on = False

self.async_write_ha_state()
if self._coordinator:
Expand Down