Extend Modbus binary sensor to support discrete inputs (#30004)

* Extend Modbus binary sensor to support discrete inputs

* Add backward compatibility for Modbus binary sensor
This commit is contained in:
Vladimír Záhradník 2020-02-10 22:56:40 +01:00 committed by GitHub
parent 4467409e5c
commit 94da129ef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 30 deletions

View File

@ -213,6 +213,12 @@ class ModbusHub:
kwargs = {"unit": unit} if unit else {} kwargs = {"unit": unit} if unit else {}
return self._client.read_coils(address, count, **kwargs) return self._client.read_coils(address, count, **kwargs)
def read_discrete_inputs(self, unit, address, count):
"""Read discrete inputs."""
with self._lock:
kwargs = {"unit": unit} if unit else {}
return self._client.read_discrete_inputs(address, count, **kwargs)
def read_input_registers(self, unit, address, count): def read_input_registers(self, unit, address, count):
"""Read input registers.""" """Read input registers."""
with self._lock: with self._lock:

View File

@ -1,4 +1,4 @@
"""Support for Modbus Coil sensors.""" """Support for Modbus Coil and Discrete Input sensors."""
import logging import logging
from typing import Optional from typing import Optional
@ -16,52 +16,72 @@ from . import CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_COIL = "coil" CONF_DEPRECATED_COIL = "coil"
CONF_COILS = "coils" CONF_DEPRECATED_COILS = "coils"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( CONF_INPUTS = "inputs"
{ CONF_INPUT_TYPE = "input_type"
vol.Required(CONF_COILS): [ CONF_ADDRESS = "address"
{
vol.Required(CONF_COIL): cv.positive_int, INPUT_TYPE_COIL = "coil"
vol.Required(CONF_NAME): cv.string, INPUT_TYPE_DISCRETE = "discrete_input"
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string, PLATFORM_SCHEMA = vol.All(
vol.Optional(CONF_SLAVE): cv.positive_int, cv.deprecated(CONF_DEPRECATED_COILS, CONF_INPUTS),
} PLATFORM_SCHEMA.extend(
] {
} vol.Required(CONF_INPUTS): [
vol.All(
cv.deprecated(CONF_DEPRECATED_COIL, CONF_ADDRESS),
vol.Schema(
{
vol.Required(CONF_ADDRESS): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Optional(CONF_SLAVE): cv.positive_int,
vol.Optional(
CONF_INPUT_TYPE, default=INPUT_TYPE_COIL
): vol.In([INPUT_TYPE_COIL, INPUT_TYPE_DISCRETE]),
}
),
)
]
}
),
) )
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Modbus binary sensors.""" """Set up the Modbus binary sensors."""
sensors = [] sensors = []
for coil in config.get(CONF_COILS): for entry in config.get(CONF_INPUTS):
hub = hass.data[MODBUS_DOMAIN][coil.get(CONF_HUB)] hub = hass.data[MODBUS_DOMAIN][entry.get(CONF_HUB)]
sensors.append( sensors.append(
ModbusCoilSensor( ModbusBinarySensor(
hub, hub,
coil.get(CONF_NAME), entry.get(CONF_NAME),
coil.get(CONF_SLAVE), entry.get(CONF_SLAVE),
coil.get(CONF_COIL), entry.get(CONF_ADDRESS),
coil.get(CONF_DEVICE_CLASS), entry.get(CONF_DEVICE_CLASS),
entry.get(CONF_INPUT_TYPE),
) )
) )
add_entities(sensors) add_entities(sensors)
class ModbusCoilSensor(BinarySensorDevice): class ModbusBinarySensor(BinarySensorDevice):
"""Modbus coil sensor.""" """Modbus binary sensor."""
def __init__(self, hub, name, slave, coil, device_class): def __init__(self, hub, name, slave, address, device_class, input_type):
"""Initialize the Modbus coil sensor.""" """Initialize the Modbus binary sensor."""
self._hub = hub self._hub = hub
self._name = name self._name = name
self._slave = int(slave) if slave else None self._slave = int(slave) if slave else None
self._coil = int(coil) self._address = int(address)
self._device_class = device_class self._device_class = device_class
self._input_type = input_type
self._value = None self._value = None
@property @property
@ -81,13 +101,16 @@ class ModbusCoilSensor(BinarySensorDevice):
def update(self): def update(self):
"""Update the state of the sensor.""" """Update the state of the sensor."""
result = self._hub.read_coils(self._slave, self._coil, 1) if self._input_type == INPUT_TYPE_COIL:
result = self._hub.read_coils(self._slave, self._address, 1)
else:
result = self._hub.read_discrete_inputs(self._slave, self._address, 1)
try: try:
self._value = result.bits[0] self._value = result.bits[0]
except AttributeError: except AttributeError:
_LOGGER.error( _LOGGER.error(
"No response from hub %s, slave %s, coil %s", "No response from hub %s, slave %s, address %s",
self._hub.name, self._hub.name,
self._slave, self._slave,
self._coil, self._address,
) )