Add device_descriptor and device_name to keyboard event (#14642)
* Add device_descriptor and device_name to keyboard event This allows automations to identify which device has generated the keypress. This is especially useful for bluetooth remotes to control different devices. * Remove line breaks * Fix
This commit is contained in:
parent
f8c8900297
commit
99fdd3e358
1 changed files with 17 additions and 32 deletions
|
@ -50,10 +50,7 @@ def setup(hass, config):
|
|||
"""Set up the keyboard_remote."""
|
||||
config = config.get(DOMAIN)
|
||||
|
||||
keyboard_remote = KeyboardRemote(
|
||||
hass,
|
||||
config
|
||||
)
|
||||
keyboard_remote = KeyboardRemote(hass, config)
|
||||
|
||||
def _start_keyboard_remote(_event):
|
||||
keyboard_remote.run()
|
||||
|
@ -61,14 +58,8 @@ def setup(hass, config):
|
|||
def _stop_keyboard_remote(_event):
|
||||
keyboard_remote.stop()
|
||||
|
||||
hass.bus.listen_once(
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
_start_keyboard_remote
|
||||
)
|
||||
hass.bus.listen_once(
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
_stop_keyboard_remote
|
||||
)
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_keyboard_remote)
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _stop_keyboard_remote)
|
||||
|
||||
return True
|
||||
|
||||
|
@ -93,10 +84,8 @@ class KeyboardRemoteThread(threading.Thread):
|
|||
_LOGGER.debug("Keyboard connected, %s", self.device_id)
|
||||
else:
|
||||
_LOGGER.debug(
|
||||
'Keyboard not connected, %s.\n\
|
||||
Check /dev/input/event* permissions.',
|
||||
self.device_id
|
||||
)
|
||||
"Keyboard not connected, %s. "
|
||||
"Check /dev/input/event* permissions", self.device_id)
|
||||
|
||||
id_folder = '/dev/input/by-id/'
|
||||
|
||||
|
@ -105,12 +94,9 @@ class KeyboardRemoteThread(threading.Thread):
|
|||
device_names = [InputDevice(file_name).name
|
||||
for file_name in list_devices()]
|
||||
_LOGGER.debug(
|
||||
'Possible device names are:\n %s.\n \
|
||||
Possible device descriptors are %s:\n %s',
|
||||
device_names,
|
||||
id_folder,
|
||||
os.listdir(id_folder)
|
||||
)
|
||||
"Possible device names are: %s. "
|
||||
"Possible device descriptors are %s: %s",
|
||||
device_names, id_folder, os.listdir(id_folder))
|
||||
|
||||
threading.Thread.__init__(self)
|
||||
self.stopped = threading.Event()
|
||||
|
@ -149,9 +135,7 @@ class KeyboardRemoteThread(threading.Thread):
|
|||
self.dev = self._get_keyboard_device()
|
||||
if self.dev is not None:
|
||||
self.dev.grab()
|
||||
self.hass.bus.fire(
|
||||
KEYBOARD_REMOTE_CONNECTED
|
||||
)
|
||||
self.hass.bus.fire(KEYBOARD_REMOTE_CONNECTED)
|
||||
_LOGGER.debug("Keyboard re-connected, %s", self.device_id)
|
||||
else:
|
||||
continue
|
||||
|
@ -160,9 +144,7 @@ class KeyboardRemoteThread(threading.Thread):
|
|||
event = self.dev.read_one()
|
||||
except IOError: # Keyboard Disconnected
|
||||
self.dev = None
|
||||
self.hass.bus.fire(
|
||||
KEYBOARD_REMOTE_DISCONNECTED
|
||||
)
|
||||
self.hass.bus.fire(KEYBOARD_REMOTE_DISCONNECTED)
|
||||
_LOGGER.debug("Keyboard disconnected, %s", self.device_id)
|
||||
continue
|
||||
|
||||
|
@ -174,7 +156,11 @@ class KeyboardRemoteThread(threading.Thread):
|
|||
_LOGGER.debug(categorize(event))
|
||||
self.hass.bus.fire(
|
||||
KEYBOARD_REMOTE_COMMAND_RECEIVED,
|
||||
{KEY_CODE: event.code}
|
||||
{
|
||||
KEY_CODE: event.code,
|
||||
DEVICE_DESCRIPTOR: self.device_descriptor,
|
||||
DEVICE_NAME: self.device_name
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
@ -191,9 +177,8 @@ class KeyboardRemote(object):
|
|||
|
||||
if device_descriptor is not None\
|
||||
or device_name is not None:
|
||||
thread = KeyboardRemoteThread(hass, device_name,
|
||||
device_descriptor,
|
||||
key_value)
|
||||
thread = KeyboardRemoteThread(
|
||||
hass, device_name, device_descriptor, key_value)
|
||||
self.threads.append(thread)
|
||||
|
||||
def run(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue