# -*- coding: utf-8 -*-
"""
Display the current screen backlight level.

Configuration parameters:
    cache_timeout: how often we refresh this module in seconds (default: 10s)
    device: The backlight device (default: "acpi_video0")
            If you are unsure try: `ls /sys/class/backlight`
    color:  The text color (default: "#FFFFFF")

Format status string parameters:
    {level} brightness

@author Tjaart van der Walt (github:tjaartvdwalt)
@license BSD
"""

from __future__ import division

import subprocess
from time import time


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    device = "acpi_video0"
    format = u'☼: {level}%'
    color = '#FFFFFF'

    def backlight(self,  i3s_output_list, i3s_config):
        level = int(subprocess.check_output(
            ["cat", "/sys/class/backlight/%s/brightness" % self.device]))
        max_level = int(subprocess.check_output(
            ["cat", "/sys/class/backlight/%s/max_brightness" % self.device]))
        full_text = self.format.format(level=(level * 100 // max_level))
        response = {
            'cached_until': time() + self.cache_timeout,
            'full_text': full_text,
            'color': self.color
        }
        return response


if __name__ == "__main__":
    from time import sleep
    x = Py3status()
    while True:
        print(x.backlight([], None))
        sleep(1)

@@ -178,170 +178,160 @@
     
-device: The backlight device (default: %22acpi_video0%22)%0A            If you are unsure try: %60ls /sys/class/backlight%60%0A    color:  The text color (default: %22#FFFFFF%22)
+color:  The text color (default: %22#FFFFFF%22)%0A    device: The backlight device%0A            If not specified the plugin will try to detect it automatically
 %0A%0AFo
@@ -493,25 +493,17 @@
 %0Aimport 
-subproces
+o
 s%0Afrom t
@@ -520,16 +520,302 @@
  time%0A%0A%0A
+def get_device_path():%0A    for (path, devices, files) in os.walk('/sys/class/backlight/'):%0A        for device in devices:%0A            if 'brightness' in os.listdir(path + device) and %5C%0A               'max_brightness' in os.listdir(path + device):%0A                return path + device%0A%0A%0A
 class Py
@@ -911,30 +911,25 @@
     
-device = %22acpi_video0%22
+color = '#FFFFFF'
 %0A   
@@ -952,38 +952,16 @@
 level%7D%25'
-%0A    color = '#FFFFFF'
 %0A%0A    de
@@ -1021,64 +1021,124 @@
     
-level = int(subprocess.check_output(%0A            %5B%22cat%22,
+if(not hasattr(self, 'device')):%0A            device_path = get_device_path()%0A        else:%0A            device_path =
  %22/s
@@ -1162,19 +1162,8 @@
 t/%25s
-/brightness
 %22 %25 
@@ -1165,35 +1165,33 @@
 s%22 %25 self.device
-%5D))
+%0A
 %0A        max_lev
@@ -1187,91 +1187,161 @@
     
-max_level = int(subprocess.check_output(%0A            %5B%22cat%22, %22/sys/class/backlight/
+for brightness_line in open(%22%25s/brightness%22 %25 device_path, 'rb'):%0A            brightness = int(brightness_line)%0A        for brightness_max_line in open(%22
 %25s/m
@@ -1361,22 +1361,82 @@
 %22 %25 
-self.
 device
-%5D))
+_path, 'rb'):%0A            brightness_max = int(brightness_max_line)%0A
 %0A   
@@ -1482,32 +1482,42 @@
 el=(
-level * 100 // max_level
+brightness * 100 // brightness_max
 ))%0A 

remove subprocess + auto detect device name