2014-04-23 20:55:22 +00:00
|
|
|
"""
|
2014-04-25 03:43:33 +00:00
|
|
|
homeassistant.components.process
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-04-23 20:55:22 +00:00
|
|
|
|
2014-04-25 03:43:33 +00:00
|
|
|
Provides functionality to watch for specific processes running
|
|
|
|
on the host machine.
|
2014-11-09 10:58:41 +00:00
|
|
|
|
|
|
|
Author: Markus Stenberg <fingon@iki.fi>
|
2014-04-23 20:55:22 +00:00
|
|
|
"""
|
2015-03-09 06:49:06 +00:00
|
|
|
import logging
|
2014-04-23 20:55:22 +00:00
|
|
|
import os
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF
|
2014-04-25 03:43:33 +00:00
|
|
|
import homeassistant.util as util
|
|
|
|
|
2014-04-23 20:55:22 +00:00
|
|
|
DOMAIN = 'process'
|
2014-08-13 12:28:45 +00:00
|
|
|
DEPENDENCIES = []
|
2014-04-23 20:55:22 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
2014-04-25 03:43:33 +00:00
|
|
|
|
2014-04-24 14:13:57 +00:00
|
|
|
PS_STRING = 'ps awx'
|
2014-04-23 20:55:22 +00:00
|
|
|
|
|
|
|
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2014-04-25 03:43:33 +00:00
|
|
|
""" Sets up a check if specified processes are running.
|
|
|
|
|
|
|
|
processes: dict mapping entity id to substring to search for
|
|
|
|
in process list.
|
|
|
|
"""
|
|
|
|
|
2015-03-09 06:49:06 +00:00
|
|
|
# Deprecated as of 3/7/2015
|
|
|
|
logging.getLogger(__name__).warning(
|
|
|
|
"This component has been deprecated and will be removed in the future."
|
|
|
|
" Please use sensor.systemmonitor with the process type")
|
|
|
|
|
2014-04-25 03:43:33 +00:00
|
|
|
entities = {ENTITY_ID_FORMAT.format(util.slugify(pname)): pstring
|
2014-08-13 12:28:45 +00:00
|
|
|
for pname, pstring in config[DOMAIN].items()}
|
2014-04-23 20:55:22 +00:00
|
|
|
|
2014-04-25 03:43:33 +00:00
|
|
|
def update_process_states(time):
|
|
|
|
""" Check ps for currently running processes and update states. """
|
2014-04-24 14:13:57 +00:00
|
|
|
with os.popen(PS_STRING, 'r') as psfile:
|
2014-04-25 03:43:33 +00:00
|
|
|
lines = list(psfile)
|
|
|
|
|
|
|
|
for entity_id, pstring in entities.items():
|
|
|
|
state = STATE_ON if any(pstring in l for l in lines) else STATE_OFF
|
|
|
|
|
|
|
|
hass.states.set(entity_id, state)
|
|
|
|
|
|
|
|
update_process_states(None)
|
|
|
|
|
|
|
|
hass.track_time_change(update_process_states, second=[0, 30])
|
|
|
|
|
2014-04-23 20:55:22 +00:00
|
|
|
return True
|