1 """Binary sensor platform integration for Numato USB GPIO expanders."""
3 from __future__
import annotations
5 from functools
import partial
8 from numato_gpio
import NumatoGpioError
27 _LOGGER = logging.getLogger(__name__)
29 NUMATO_SIGNAL =
"numato_signal_{}_{}"
35 add_entities: AddEntitiesCallback,
36 discovery_info: DiscoveryInfoType |
None =
None,
38 """Set up the configured Numato USB GPIO binary sensor ports."""
39 if discovery_info
is None:
42 def read_gpio(device_id: int, port: int, level: bool) ->
None:
43 """Send signal to entity to have it update state."""
46 api = hass.data[DOMAIN][DATA_API]
48 devices = hass.data[DOMAIN][CONF_DEVICES]
49 for device
in [d
for d
in devices
if CONF_BINARY_SENSORS
in d]:
50 device_id = device[CONF_ID]
51 platform = device[CONF_BINARY_SENSORS]
52 invert_logic = platform[CONF_INVERT_LOGIC]
53 ports = platform[CONF_PORTS]
54 for port, port_name
in ports.items():
56 api.setup_input(device_id, port)
58 except NumatoGpioError
as err:
61 "Failed to initialize binary sensor '%s' on Numato device %s"
71 api.edge_detect(device_id, port, partial(read_gpio, device_id))
73 except NumatoGpioError
as err:
75 "Notification setup failed on device %s, "
76 "updates on binary sensor %s only in polling mode: %s",
81 binary_sensors.append(
94 """Represents a binary sensor (input) port of a Numato GPIO expander."""
96 _attr_should_poll =
False
98 def __init__(self, name, device_id, port, invert_logic, api):
99 """Initialize the Numato GPIO based binary sensor object."""
108 """Connect state update callback."""
119 """Update entity state."""
125 """Return the state of the entity."""
129 """Update the GPIO state."""
132 except NumatoGpioError
as err:
135 "Failed to update Numato device %s port %s: %s",
def __init__(self, name, device_id, port, invert_logic, api)
def _async_update_state(self, level)
None async_added_to_hass(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
None dispatcher_send(HomeAssistant hass, str signal, *Any args)