2019-02-13 20:21:14 +00:00
|
|
|
"""Support for raspihats board binary sensors."""
|
2017-05-12 16:20:48 +00:00
|
|
|
import logging
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2017-05-12 16:20:48 +00:00
|
|
|
import voluptuous as vol
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ADDRESS,
|
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_NAME,
|
|
|
|
DEVICE_DEFAULT_NAME,
|
|
|
|
)
|
2018-01-21 06:35:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-05-12 16:20:48 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_BOARD,
|
|
|
|
CONF_CHANNELS,
|
|
|
|
CONF_I2C_HATS,
|
|
|
|
CONF_INDEX,
|
|
|
|
CONF_INVERT_LOGIC,
|
|
|
|
I2C_HAT_NAMES,
|
|
|
|
I2C_HATS_MANAGER,
|
|
|
|
I2CHatsException,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-05-12 16:20:48 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_INVERT_LOGIC = False
|
|
|
|
DEFAULT_DEVICE_CLASS = None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_CHANNELS_SCHEMA = vol.Schema(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
vol.Required(CONF_INDEX): cv.positive_int,
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS, default=DEFAULT_DEVICE_CLASS): cv.string,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
_I2C_HATS_SCHEMA = vol.Schema(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
vol.Required(CONF_BOARD): vol.In(I2C_HAT_NAMES),
|
|
|
|
vol.Required(CONF_ADDRESS): vol.Coerce(int),
|
|
|
|
vol.Required(CONF_CHANNELS): _CHANNELS_SCHEMA,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_I2C_HATS): _I2C_HATS_SCHEMA}
|
|
|
|
)
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the raspihats binary_sensor devices."""
|
2017-05-12 16:20:48 +00:00
|
|
|
I2CHatBinarySensor.I2C_HATS_MANAGER = hass.data[I2C_HATS_MANAGER]
|
|
|
|
binary_sensors = []
|
|
|
|
i2c_hat_configs = config.get(CONF_I2C_HATS)
|
|
|
|
for i2c_hat_config in i2c_hat_configs:
|
|
|
|
address = i2c_hat_config[CONF_ADDRESS]
|
|
|
|
board = i2c_hat_config[CONF_BOARD]
|
|
|
|
try:
|
|
|
|
I2CHatBinarySensor.I2C_HATS_MANAGER.register_board(board, address)
|
|
|
|
for channel_config in i2c_hat_config[CONF_CHANNELS]:
|
|
|
|
binary_sensors.append(
|
|
|
|
I2CHatBinarySensor(
|
|
|
|
address,
|
|
|
|
channel_config[CONF_INDEX],
|
|
|
|
channel_config[CONF_NAME],
|
|
|
|
channel_config[CONF_INVERT_LOGIC],
|
2019-07-31 19:25:30 +00:00
|
|
|
channel_config[CONF_DEVICE_CLASS],
|
2017-05-12 16:20:48 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
except I2CHatsException as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Failed to register %s I2CHat@%s %s", board, hex(address), str(ex)
|
|
|
|
)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(binary_sensors)
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class I2CHatBinarySensor(BinarySensorEntity):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Representation of a binary sensor that uses a I2C-HAT digital input."""
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
I2C_HATS_MANAGER = None
|
|
|
|
|
|
|
|
def __init__(self, address, channel, name, invert_logic, device_class):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the raspihats sensor."""
|
2017-05-12 16:20:48 +00:00
|
|
|
self._address = address
|
|
|
|
self._channel = channel
|
|
|
|
self._name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._invert_logic = invert_logic
|
|
|
|
self._device_class = device_class
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.I2C_HATS_MANAGER.read_di(self._address, self._channel)
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
def online_callback():
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call fired when board is online."""
|
2017-05-12 16:20:48 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
self.I2C_HATS_MANAGER.register_online_callback(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._address, self._channel, online_callback
|
|
|
|
)
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
def edge_callback(state):
|
|
|
|
"""Read digital input state."""
|
|
|
|
self._state = state
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
self.I2C_HATS_MANAGER.register_di_callback(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._address, self._channel, edge_callback
|
|
|
|
)
|
2017-05-12 16:20:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""No polling needed for this sensor."""
|
2017-05-12 16:20:48 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the state of this sensor."""
|
|
|
|
return self._state != self._invert_logic
|