2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2015-05-13 17:18:30 +00:00
|
|
|
homeassistant.components.keyboard
|
2013-12-11 08:07:30 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to emulate keyboard presses on host machine.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
SERVICE_VOLUME_UP, SERVICE_VOLUME_DOWN, SERVICE_VOLUME_MUTE,
|
|
|
|
SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PREV_TRACK,
|
|
|
|
SERVICE_MEDIA_PLAY_PAUSE)
|
|
|
|
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2014-01-05 01:55:05 +00:00
|
|
|
DOMAIN = "keyboard"
|
2014-08-13 12:28:45 +00:00
|
|
|
DEPENDENCIES = []
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def volume_up(hass):
|
2014-03-12 05:45:05 +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):
|
2014-03-12 05:45:05 +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):
|
2014-03-12 05:45:05 +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):
|
2014-03-12 05:45:05 +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):
|
2014-03-12 05:45:05 +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):
|
2014-03-12 05:45:05 +00:00
|
|
|
""" Press the keyboard button for prev track. """
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_MEDIA_PREV_TRACK)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Listen for keyboard events. """
|
|
|
|
try:
|
|
|
|
import pykeyboard
|
|
|
|
except ImportError:
|
|
|
|
logging.getLogger(__name__).exception(
|
2014-08-13 12:28:45 +00:00
|
|
|
"Error while importing dependency PyUserInput.")
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
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:
|
|
|
|
keyboard.tap_key(keyboard.volume_up_key))
|
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:
|
|
|
|
keyboard.tap_key(keyboard.volume_down_key))
|
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:
|
|
|
|
keyboard.tap_key(keyboard.volume_mute_key))
|
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:
|
|
|
|
keyboard.tap_key(keyboard.media_play_pause_key))
|
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:
|
|
|
|
keyboard.tap_key(keyboard.media_next_track_key))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_MEDIA_PREV_TRACK,
|
2014-04-24 07:40:45 +00:00
|
|
|
lambda service:
|
|
|
|
keyboard.tap_key(keyboard.media_prev_track_key))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return True
|