2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
Provides functionality to emulate keyboard presses on host machine.
|
2015-10-25 14:10:07 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/keyboard/
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2016-04-13 02:13:40 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2016-02-19 05:27:50 +00:00
|
|
|
SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PLAY_PAUSE,
|
|
|
|
SERVICE_MEDIA_PREVIOUS_TRACK, SERVICE_VOLUME_DOWN, SERVICE_VOLUME_MUTE,
|
|
|
|
SERVICE_VOLUME_UP)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2016-09-01 08:37:58 +00:00
|
|
|
REQUIREMENTS = ['pyuserinput==0.1.11']
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def volume_up(hass):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Press the keyboard button for volume up."""
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_VOLUME_UP)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def volume_down(hass):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Press the keyboard button for volume down."""
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_VOLUME_DOWN)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def volume_mute(hass):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Press the keyboard button for muting volume."""
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_VOLUME_MUTE)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def media_play_pause(hass):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Press the keyboard button for play/pause."""
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY_PAUSE)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def media_next_track(hass):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Press the keyboard button for next track."""
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_MEDIA_NEXT_TRACK)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def media_prev_track(hass):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Press the keyboard button for prev track."""
|
2015-06-01 07:24:17 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Listen for keyboard events."""
|
2016-01-29 05:37:08 +00:00
|
|
|
import pykeyboard
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
keyboard = pykeyboard.PyKeyboard()
|
|
|
|
keyboard.special_key_assignment()
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_VOLUME_UP,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.volume_up_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_VOLUME_DOWN,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.volume_down_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_VOLUME_MUTE,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.volume_mute_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_MEDIA_PLAY_PAUSE,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.media_play_pause_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_MEDIA_NEXT_TRACK,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.media_next_track_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-06-01 07:24:17 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
2016-04-13 02:13:40 +00:00
|
|
|
keyboard.tap_key(keyboard.media_prev_track_key),
|
|
|
|
schema=TAP_KEY_SCHEMA)
|
2013-12-11 08:07:30 +00:00
|
|
|
return True
|