2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Modbus Coil sensors."""
|
2016-09-13 20:47:44 +00:00
|
|
|
import logging
|
2019-12-21 09:54:48 +00:00
|
|
|
from typing import Optional
|
2019-02-24 09:22:17 +00:00
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-21 09:54:48 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASSES_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BinarySensorDevice,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME, CONF_SLAVE
|
2016-09-13 20:47:44 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN
|
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_COIL = "coil"
|
|
|
|
CONF_COILS = "coils"
|
2016-09-13 20:47:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_COILS): [
|
|
|
|
{
|
|
|
|
vol.Required(CONF_COIL): cv.positive_int,
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
2019-12-21 09:54:48 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
|
|
|
|
vol.Optional(CONF_SLAVE): cv.positive_int,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
2016-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, 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):
|
2019-02-11 19:00:37 +00:00
|
|
|
hub = hass.data[MODBUS_DOMAIN][coil.get(CONF_HUB)]
|
2019-07-31 19:25:30 +00:00
|
|
|
sensors.append(
|
|
|
|
ModbusCoilSensor(
|
2019-12-21 09:54:48 +00:00
|
|
|
hub,
|
|
|
|
coil.get(CONF_NAME),
|
|
|
|
coil.get(CONF_SLAVE),
|
|
|
|
coil.get(CONF_COIL),
|
|
|
|
coil.get(CONF_DEVICE_CLASS),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2019-02-24 09:22:17 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
2016-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModbusCoilSensor(BinarySensorDevice):
|
|
|
|
"""Modbus coil sensor."""
|
|
|
|
|
2019-12-21 09:54:48 +00:00
|
|
|
def __init__(self, hub, name, slave, coil, device_class):
|
2019-02-24 09:22:17 +00:00
|
|
|
"""Initialize the Modbus coil sensor."""
|
2019-02-11 19:00:37 +00:00
|
|
|
self._hub = hub
|
2016-09-13 20:47:44 +00:00
|
|
|
self._name = name
|
|
|
|
self._slave = int(slave) if slave else None
|
|
|
|
self._coil = int(coil)
|
2019-12-21 09:54:48 +00:00
|
|
|
self._device_class = device_class
|
2016-09-13 20:47:44 +00:00
|
|
|
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
|
|
|
|
|
2019-12-21 09:54:48 +00:00
|
|
|
@property
|
|
|
|
def device_class(self) -> Optional[str]:
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update the state of the sensor."""
|
2019-02-11 19:00:37 +00:00
|
|
|
result = self._hub.read_coils(self._slave, self._coil, 1)
|
2017-07-07 05:30:23 +00:00
|
|
|
try:
|
|
|
|
self._value = result.bits[0]
|
|
|
|
except AttributeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"No response from hub %s, slave %s, coil %s",
|
|
|
|
self._hub.name,
|
|
|
|
self._slave,
|
|
|
|
self._coil,
|
|
|
|
)
|