Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple process state monitor source. #3

Merged
merged 3 commits into from
Apr 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ def get_hosts(section):

add_status("WeMo", wemo.setup(hass, hosts))

# Process tracking
if has_section("process"):
process = load_module('process')

processes = dict(config.items('process'))
add_status("process", process.setup(hass, processes))

# Light control
if has_section("light.hue"):
light = load_module('light')
Expand Down
51 changes: 51 additions & 0 deletions homeassistant/components/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- Python -*-
#
# $Id: process.py $
#
# Author: Markus Stenberg <fingon@iki.fi>
#
# Copyright (c) 2014 Markus Stenberg
#
# Created: Wed Apr 23 23:33:26 2014 mstenber
# Last modified: Thu Apr 24 17:13:04 2014 mstenber
# Edit time: 19 min
#
"""

Process watcher.

The arguments are <subentityname>=<substring to find in process list>

"""

from homeassistant.components import (STATE_ON, STATE_OFF)
import os

DOMAIN = 'process'
ENTITY_ID_FORMAT = DOMAIN + '.{}'
PS_STRING = 'ps awx'


def setup(hass, processes):
""" Track local processes. """

# pylint: disable=unused-argument
def _update_process_state(time):
""" Check ps for currently running processes. """
with os.popen(PS_STRING, 'r') as psfile:
lines = list(iter(psfile))
for pname, pstring in processes.items():
found = False
for line in lines:
if pstring in line:
found = True
break
entity_id = ENTITY_ID_FORMAT.format(pname)
state = found and STATE_ON or STATE_OFF
hass.states.set(entity_id, state)

_update_process_state(None)
hass.track_time_change(_update_process_state, second=[0, 30])
return True