2019-02-14 04:35:12 +00:00
|
|
|
"""Support to emulate keyboard presses on host machine."""
|
2019-10-17 12:58:56 +00:00
|
|
|
from pykeyboard import PyKeyboard # pylint: disable=import-error
|
2016-04-13 02:13:40 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_MEDIA_NEXT_TRACK,
|
|
|
|
SERVICE_MEDIA_PLAY_PAUSE,
|
|
|
|
SERVICE_MEDIA_PREVIOUS_TRACK,
|
|
|
|
SERVICE_VOLUME_DOWN,
|
|
|
|
SERVICE_VOLUME_MUTE,
|
|
|
|
SERVICE_VOLUME_UP,
|
|
|
|
)
|
2022-01-02 15:34:33 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "keyboard"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-04-13 02:13:40 +00:00
|
|
|
TAP_KEY_SCHEMA = vol.Schema({})
|
|
|
|
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2022-01-02 15:34:33 +00:00
|
|
|
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Listen for keyboard events."""
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2019-10-17 12:58:56 +00:00
|
|
|
keyboard = PyKeyboard()
|
2013-12-11 08:07:30 +00:00
|
|
|
keyboard.special_key_assignment()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_VOLUME_UP,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.volume_up_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_VOLUME_DOWN,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.volume_down_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_VOLUME_MUTE,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.volume_mute_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_MEDIA_PLAY_PAUSE,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.media_play_pause_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_MEDIA_NEXT_TRACK,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.media_next_track_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_MEDIA_PREVIOUS_TRACK,
|
|
|
|
lambda service: keyboard.tap_key(keyboard.media_prev_track_key),
|
|
|
|
schema=TAP_KEY_SCHEMA,
|
|
|
|
)
|
2013-12-11 08:07:30 +00:00
|
|
|
return True
|