From e36a53eea68be83be08c72e65c8d930cb33d6855 Mon Sep 17 00:00:00 2001 From: Markus Stenberg Date: Wed, 23 Apr 2014 23:55:22 +0300 Subject: [PATCH 1/2] Added simple process state monitor source. --- homeassistant/bootstrap.py | 7 ++++ homeassistant/components/process.py | 59 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 homeassistant/components/process.py diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 48933f8c87c..11ab16fcefd 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -134,6 +134,13 @@ def from_config_file(config_path): add_status("WeMo", wemo.setup(bus, statemachine)) + # Process tracking + if has_section("process"): + process = load_module('process') + + kwargs = dict(config.items('process')) + add_status("process", process.setup(bus, statemachine, **kwargs)) + # Light control if has_section("light.hue"): light = load_module('light') diff --git a/homeassistant/components/process.py b/homeassistant/components/process.py new file mode 100644 index 00000000000..912a1ab0569 --- /dev/null +++ b/homeassistant/components/process.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# -*- Python -*- +# +# $Id: process.py $ +# +# Author: Markus Stenberg +# +# Copyright (c) 2014 Markus Stenberg +# +# Created: Wed Apr 23 23:33:26 2014 mstenber +# Last modified: Wed Apr 23 23:48:13 2014 mstenber +# Edit time: 13 min +# +""" + +Process watcher. + +The arguments are = + +""" + +import homeassistant as ha +from homeassistant.components import (STATE_ON, STATE_OFF) +import os + +DOMAIN = 'process' +ENTITY_ID_FORMAT = DOMAIN + '.{}' + +PS_STRING='ps awx' + +INTERVAL=30 + +def setup(bus, statemachine, **processes): + _states = {} + + def _update_process_state(t, force_reload=False): + with os.popen(PS_STRING, 'r') as f: + lines = list(iter(f)) + for e, s in processes.items(): + found = False + for line in lines: + if s in line: + found = True + break + if _states.get(e, None) == found: + continue + _states[e] = found + entity_id = ENTITY_ID_FORMAT.format(e) + state = found and STATE_ON or STATE_OFF + statemachine.set_state(entity_id, state) + + _update_process_state(None, True) + kwargs = {} + if INTERVAL != ha.TIMER_INTERVAL: + kwargs['second'] = [0, INTERVAL] + assert INTERVAL > ha.TIMER_INTERVAL + ha.track_time_change(bus, _update_process_state) + return True From 88fd75b4c7cada52a3aeacc0a1e20f1acb79e829 Mon Sep 17 00:00:00 2001 From: Markus Stenberg Date: Thu, 24 Apr 2014 17:13:57 +0300 Subject: [PATCH 2/2] Addressed the comments in the issue. pep8+pylint seem fine now. (still works, too.) --- homeassistant/bootstrap.py | 4 +-- homeassistant/components/process.py | 40 ++++++++++++----------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index c0e434ffd81..9f4a3f1b2b0 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -150,8 +150,8 @@ def from_config_file(config_path, enable_logging=True): if has_section("process"): process = load_module('process') - kwargs = dict(config.items('process')) - add_status("process", process.setup(bus, statemachine, **kwargs)) + processes = dict(config.items('process')) + add_status("process", process.setup(hass, processes)) # Light control if has_section("light.hue"): diff --git a/homeassistant/components/process.py b/homeassistant/components/process.py index 912a1ab0569..eef3ccc4b89 100644 --- a/homeassistant/components/process.py +++ b/homeassistant/components/process.py @@ -9,8 +9,8 @@ # Copyright (c) 2014 Markus Stenberg # # Created: Wed Apr 23 23:33:26 2014 mstenber -# Last modified: Wed Apr 23 23:48:13 2014 mstenber -# Edit time: 13 min +# Last modified: Thu Apr 24 17:13:04 2014 mstenber +# Edit time: 19 min # """ @@ -20,40 +20,32 @@ The arguments are = """ -import homeassistant as ha from homeassistant.components import (STATE_ON, STATE_OFF) import os DOMAIN = 'process' ENTITY_ID_FORMAT = DOMAIN + '.{}' +PS_STRING = 'ps awx' -PS_STRING='ps awx' -INTERVAL=30 +def setup(hass, processes): + """ Track local processes. """ -def setup(bus, statemachine, **processes): - _states = {} - - def _update_process_state(t, force_reload=False): - with os.popen(PS_STRING, 'r') as f: - lines = list(iter(f)) - for e, s in processes.items(): + # 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 s in line: + if pstring in line: found = True break - if _states.get(e, None) == found: - continue - _states[e] = found - entity_id = ENTITY_ID_FORMAT.format(e) + entity_id = ENTITY_ID_FORMAT.format(pname) state = found and STATE_ON or STATE_OFF - statemachine.set_state(entity_id, state) + hass.states.set(entity_id, state) - _update_process_state(None, True) - kwargs = {} - if INTERVAL != ha.TIMER_INTERVAL: - kwargs['second'] = [0, INTERVAL] - assert INTERVAL > ha.TIMER_INTERVAL - ha.track_time_change(bus, _update_process_state) + _update_process_state(None) + hass.track_time_change(_update_process_state, second=[0, 30]) return True