Fix Insteon open issues with adding devices by address and missing events (#89305)

* Add missing events

* Bump dependancies

* Update for code review
This commit is contained in:
Tom Harris 2023-03-07 21:06:29 -05:00 committed by Franck Nijhof
parent 74d3b2374b
commit 52cd2f9429
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
4 changed files with 37 additions and 32 deletions

View File

@ -17,8 +17,8 @@
"iot_class": "local_push", "iot_class": "local_push",
"loggers": ["pyinsteon", "pypubsub"], "loggers": ["pyinsteon", "pypubsub"],
"requirements": [ "requirements": [
"pyinsteon==1.3.3", "pyinsteon==1.3.4",
"insteon-frontend-home-assistant==0.3.2" "insteon-frontend-home-assistant==0.3.3"
], ],
"usb": [ "usb": [
{ {

View File

@ -1,11 +1,13 @@
"""Utilities used by insteon component.""" """Utilities used by insteon component."""
import asyncio import asyncio
from collections.abc import Callable
import logging import logging
from pyinsteon import devices from pyinsteon import devices
from pyinsteon.address import Address from pyinsteon.address import Address
from pyinsteon.constants import ALDBStatus, DeviceAction from pyinsteon.constants import ALDBStatus, DeviceAction
from pyinsteon.events import OFF_EVENT, OFF_FAST_EVENT, ON_EVENT, ON_FAST_EVENT from pyinsteon.device_types.device_base import Device
from pyinsteon.events import OFF_EVENT, OFF_FAST_EVENT, ON_EVENT, ON_FAST_EVENT, Event
from pyinsteon.managers.link_manager import ( from pyinsteon.managers.link_manager import (
async_enter_linking_mode, async_enter_linking_mode,
async_enter_unlinking_mode, async_enter_unlinking_mode,
@ -27,7 +29,7 @@ from homeassistant.const import (
CONF_PLATFORM, CONF_PLATFORM,
ENTITY_MATCH_ALL, ENTITY_MATCH_ALL,
) )
from homeassistant.core import ServiceCall, callback from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
@ -89,49 +91,52 @@ from .schemas import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def add_on_off_event_device(hass, device): def _register_event(event: Event, listener: Callable) -> None:
"""Register the events raised by a device."""
_LOGGER.debug(
"Registering on/off event for %s %d %s",
str(event.address),
event.group,
event.name,
)
event.subscribe(listener, force_strong_ref=True)
def add_on_off_event_device(hass: HomeAssistant, device: Device) -> None:
"""Register an Insteon device as an on/off event device.""" """Register an Insteon device as an on/off event device."""
@callback @callback
def async_fire_group_on_off_event(name, address, group, button): def async_fire_group_on_off_event(
name: str, address: Address, group: int, button: str
):
# Firing an event when a button is pressed. # Firing an event when a button is pressed.
if button and button[-2] == "_": if button and button[-2] == "_":
button_id = button[-1].lower() button_id = button[-1].lower()
else: else:
button_id = None button_id = None
schema = {CONF_ADDRESS: address} schema = {CONF_ADDRESS: address, "group": group}
if button_id: if button_id:
schema[EVENT_CONF_BUTTON] = button_id schema[EVENT_CONF_BUTTON] = button_id
if name == ON_EVENT: if name == ON_EVENT:
event = EVENT_GROUP_ON event = EVENT_GROUP_ON
if name == OFF_EVENT: elif name == OFF_EVENT:
event = EVENT_GROUP_OFF event = EVENT_GROUP_OFF
if name == ON_FAST_EVENT: elif name == ON_FAST_EVENT:
event = EVENT_GROUP_ON_FAST event = EVENT_GROUP_ON_FAST
if name == OFF_FAST_EVENT: elif name == OFF_FAST_EVENT:
event = EVENT_GROUP_OFF_FAST event = EVENT_GROUP_OFF_FAST
else:
event = f"insteon.{name}"
_LOGGER.debug("Firing event %s with %s", event, schema) _LOGGER.debug("Firing event %s with %s", event, schema)
hass.bus.async_fire(event, schema) hass.bus.async_fire(event, schema)
for group in device.events: for name_or_group, event in device.events.items():
if isinstance(group, int): if isinstance(name_or_group, int):
for event in device.events[group]: for _, event in device.events[name_or_group].items():
if event in [ _register_event(event, async_fire_group_on_off_event)
OFF_EVENT, else:
ON_EVENT, _register_event(event, async_fire_group_on_off_event)
OFF_FAST_EVENT,
ON_FAST_EVENT,
]:
_LOGGER.debug(
"Registering on/off event for %s %d %s",
str(device.address),
group,
event,
)
device.events[group][event].subscribe(
async_fire_group_on_off_event, force_strong_ref=True
)
def register_new_device_callback(hass): def register_new_device_callback(hass):

View File

@ -979,7 +979,7 @@ influxdb==5.3.1
inkbird-ble==0.5.6 inkbird-ble==0.5.6
# homeassistant.components.insteon # homeassistant.components.insteon
insteon-frontend-home-assistant==0.3.2 insteon-frontend-home-assistant==0.3.3
# homeassistant.components.intellifire # homeassistant.components.intellifire
intellifire4py==2.2.2 intellifire4py==2.2.2
@ -1687,7 +1687,7 @@ pyialarm==2.2.0
pyicloud==1.0.0 pyicloud==1.0.0
# homeassistant.components.insteon # homeassistant.components.insteon
pyinsteon==1.3.3 pyinsteon==1.3.4
# homeassistant.components.intesishome # homeassistant.components.intesishome
pyintesishome==1.8.0 pyintesishome==1.8.0

View File

@ -738,7 +738,7 @@ influxdb==5.3.1
inkbird-ble==0.5.6 inkbird-ble==0.5.6
# homeassistant.components.insteon # homeassistant.components.insteon
insteon-frontend-home-assistant==0.3.2 insteon-frontend-home-assistant==0.3.3
# homeassistant.components.intellifire # homeassistant.components.intellifire
intellifire4py==2.2.2 intellifire4py==2.2.2
@ -1212,7 +1212,7 @@ pyialarm==2.2.0
pyicloud==1.0.0 pyicloud==1.0.0
# homeassistant.components.insteon # homeassistant.components.insteon
pyinsteon==1.3.3 pyinsteon==1.3.4
# homeassistant.components.ipma # homeassistant.components.ipma
pyipma==3.0.6 pyipma==3.0.6