2016-09-13 20:47:44 +00:00
|
|
|
"""
|
|
|
|
Support for Modbus Coil sensors.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.modbus/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.components.modbus as modbus
|
2017-07-04 15:01:35 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_SLAVE
|
2016-09-13 20:47:44 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEPENDENCIES = ['modbus']
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
CONF_COIL = 'coil'
|
|
|
|
CONF_COILS = 'coils'
|
2016-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_COILS): [{
|
|
|
|
vol.Required(CONF_COIL): cv.positive_int,
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_SLAVE): cv.positive_int
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Modbus binary sensors."""
|
2016-09-13 20:47:44 +00:00
|
|
|
sensors = []
|
|
|
|
for coil in config.get(CONF_COILS):
|
|
|
|
sensors.append(ModbusCoilSensor(
|
|
|
|
coil.get(CONF_NAME),
|
|
|
|
coil.get(CONF_SLAVE),
|
|
|
|
coil.get(CONF_COIL)))
|
|
|
|
add_devices(sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class ModbusCoilSensor(BinarySensorDevice):
|
|
|
|
"""Modbus coil sensor."""
|
|
|
|
|
|
|
|
def __init__(self, name, slave, coil):
|
|
|
|
"""Initialize the modbus coil sensor."""
|
|
|
|
self._name = name
|
|
|
|
self._slave = int(slave) if slave else None
|
|
|
|
self._coil = int(coil)
|
|
|
|
self._value = None
|
|
|
|
|
2017-07-07 05:30:23 +00:00
|
|
|
@property
|
2017-07-01 06:32:10 +00:00
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._value
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the state of the sensor."""
|
|
|
|
result = modbus.HUB.read_coils(self._slave, self._coil, 1)
|
2017-07-07 05:30:23 +00:00
|
|
|
try:
|
|
|
|
self._value = result.bits[0]
|
|
|
|
except AttributeError:
|
|
|
|
_LOGGER.error(
|
|
|
|
'No response from modbus slave %s coil %s',
|
|
|
|
self._slave,
|
|
|
|
self._coil)
|