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."""
|
"""Set up the keyboard_remote."""
|
||||||
config = config.get(DOMAIN)
|
config = config.get(DOMAIN)
|
||||||
|
|
||||||
keyboard_remote = KeyboardRemote(
|
keyboard_remote = KeyboardRemote(hass, config)
|
||||||
hass,
|
|
||||||
config
|
|
||||||
)
|
|
||||||
|
|
||||||
def _start_keyboard_remote(_event):
|
def _start_keyboard_remote(_event):
|
||||||
keyboard_remote.run()
|
keyboard_remote.run()
|
||||||
|
@ -61,14 +58,8 @@ def setup(hass, config):
|
||||||
def _stop_keyboard_remote(_event):
|
def _stop_keyboard_remote(_event):
|
||||||
keyboard_remote.stop()
|
keyboard_remote.stop()
|
||||||
|
|
||||||
hass.bus.listen_once(
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_keyboard_remote)
|
||||||
EVENT_HOMEASSISTANT_START,
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _stop_keyboard_remote)
|
||||||
_start_keyboard_remote
|
|
||||||
)
|
|
||||||
hass.bus.listen_once(
|
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
|
||||||
_stop_keyboard_remote
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -93,10 +84,8 @@ class KeyboardRemoteThread(threading.Thread):
|
||||||
_LOGGER.debug("Keyboard connected, %s", self.device_id)
|
_LOGGER.debug("Keyboard connected, %s", self.device_id)
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
'Keyboard not connected, %s.\n\
|
"Keyboard not connected, %s. "
|
||||||
Check /dev/input/event* permissions.',
|
"Check /dev/input/event* permissions", self.device_id)
|
||||||
self.device_id
|
|
||||||
)
|
|
||||||
|
|
||||||
id_folder = '/dev/input/by-id/'
|
id_folder = '/dev/input/by-id/'
|
||||||
|
|
||||||
|
@ -105,12 +94,9 @@ class KeyboardRemoteThread(threading.Thread):
|
||||||
device_names = [InputDevice(file_name).name
|
device_names = [InputDevice(file_name).name
|
||||||
for file_name in list_devices()]
|
for file_name in list_devices()]
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
'Possible device names are:\n %s.\n \
|
"Possible device names are: %s. "
|
||||||
Possible device descriptors are %s:\n %s',
|
"Possible device descriptors are %s: %s",
|
||||||
device_names,
|
device_names, id_folder, os.listdir(id_folder))
|
||||||
id_folder,
|
|
||||||
os.listdir(id_folder)
|
|
||||||
)
|
|
||||||
|
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.stopped = threading.Event()
|
self.stopped = threading.Event()
|
||||||
|
@ -149,9 +135,7 @@ class KeyboardRemoteThread(threading.Thread):
|
||||||
self.dev = self._get_keyboard_device()
|
self.dev = self._get_keyboard_device()
|
||||||
if self.dev is not None:
|
if self.dev is not None:
|
||||||
self.dev.grab()
|
self.dev.grab()
|
||||||
self.hass.bus.fire(
|
self.hass.bus.fire(KEYBOARD_REMOTE_CONNECTED)
|
||||||
KEYBOARD_REMOTE_CONNECTED
|
|
||||||
)
|
|
||||||
_LOGGER.debug("Keyboard re-connected, %s", self.device_id)
|
_LOGGER.debug("Keyboard re-connected, %s", self.device_id)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
@ -160,9 +144,7 @@ class KeyboardRemoteThread(threading.Thread):
|
||||||
event = self.dev.read_one()
|
event = self.dev.read_one()
|
||||||
except IOError: # Keyboard Disconnected
|
except IOError: # Keyboard Disconnected
|
||||||
self.dev = None
|
self.dev = None
|
||||||
self.hass.bus.fire(
|
self.hass.bus.fire(KEYBOARD_REMOTE_DISCONNECTED)
|
||||||
KEYBOARD_REMOTE_DISCONNECTED
|
|
||||||
)
|
|
||||||
_LOGGER.debug("Keyboard disconnected, %s", self.device_id)
|
_LOGGER.debug("Keyboard disconnected, %s", self.device_id)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -174,7 +156,11 @@ class KeyboardRemoteThread(threading.Thread):
|
||||||
_LOGGER.debug(categorize(event))
|
_LOGGER.debug(categorize(event))
|
||||||
self.hass.bus.fire(
|
self.hass.bus.fire(
|
||||||
KEYBOARD_REMOTE_COMMAND_RECEIVED,
|
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\
|
if device_descriptor is not None\
|
||||||
or device_name is not None:
|
or device_name is not None:
|
||||||
thread = KeyboardRemoteThread(hass, device_name,
|
thread = KeyboardRemoteThread(
|
||||||
device_descriptor,
|
hass, device_name, device_descriptor, key_value)
|
||||||
key_value)
|
|
||||||
self.threads.append(thread)
|
self.threads.append(thread)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue