# -*- coding: utf-8 -*-
'''
Philips HUE lamps module for proxy.
'''

from __future__ import absolute_import

# Import python libs
import logging

__proxyenabled__ = ['philips_hue']

GRAINS_CACHE = {}
log = logging.getLogger(__file__)


class Const:
    '''
    Constants for the lamp operations.
    '''
    LAMP_ON = {"on": True, "transitiontime": 0}
    LAMP_OFF = {"on": False, "transitiontime": 0}


def __virtual__():
    '''
    Validate the module.
    '''
    return True


def init(opts):
    '''
    Initialize the module.
    '''


def grains():
    '''
    Get the grains from the proxied device
    '''
    return grains_refresh()


def grains_refresh():
    '''
    Refresh the grains from the proxied device
    '''
    if not GRAINS_CACHE:
        GRAINS_CACHE['vendor'] = 'Philips'
        GRAINS_CACHE['product'] = 'Hue Lamps'
        
    return GRAINS_CACHE


def ping(*args, **kw):
    '''
    Ping the lamps.
    '''
    # Here blink them
    return True


def shutdown(opts, *args, **kw):
    '''
    Shuts down the service.
    '''
    # This is no-op method, which is required but makes nothing at this point.
    return True


def _set(lamp_id, state):
    '''
    Set state to the device by ID.

    :param lamp_id:
    :param state:
    :return:
    '''
    res = json.loads(requests.put(CONFIG['url']+"/lights/"
                                   + str(lamp_id) + "/state", json=state).content)
    res = len(res) > 1 and res[-1] or res[0]
    if res.get('success'):
        res = {'result': True}
    elif res.get('error'):
        res = {'result': False,
               'description': res['error']['description'],
               'type': res['error']['type']}

    return res

def _get_devices(params):
    '''
    Parse device(s) ID(s) from the common params.

    :param params:
    :return:
    '''
    if 'id' not in params:
        raise CommandExecutionError("Parameter ID is required.")

    return [int(dev) for dev in params['id'].split(",")]


# Callers
def call_lights(*args, **kwargs):
    '''
    Get info about available lamps.
    '''
    return json.loads(requests.get(CONFIG['url'] + "/lights").content)


def call_ping(*args, **kwargs):
    '''
    Ping the lamps
    '''
    ping(*args, **kw)


def call_status(*args, **kwargs):
    '''
    Return lamps status.
    '''
    return {
        1: True,
        2: True,
        3: False,
        }


def call_alert(*args, **kw):
    '''
    Blink the alert.
    '''
    return {
        1: 'Alerted',
        2: 'Alerted',
        3: 'Skipped',
    }

@@ -138,16 +138,95 @@
 logging%0A
+import requests%0Aimport json%0Afrom salt.exceptions import CommandExecutionError%0A%0A
 %0A__proxy
@@ -272,16 +272,28 @@
 HE = %7B%7D%0A
+CONFIG = %7B%7D%0A
 log = lo
@@ -2233,24 +2233,743 @@
 .content)%0A%0A%0A
+def call_switch(*args, **kwargs):%0A    '''%0A    Switch lamp ON/OFF.%0A%0A    If no particular state is passed,%0A    then lamp will be switched to the opposite state.%0A%0A    Required parameters:%0A%0A    * **id**: Specifies a device ID. Can be a comma-separated values.%0A%0A    Options:%0A%0A    * **on**: True or False%0A%0A    CLI Example:%0A%0A    .. code-block:: bash%0A%0A        salt '*' hue.switch id=1 on=True%0A        salt '*' hue.switch id=1,2,3 on=True%0A    '''%0A    if 'on' not in kwargs:%0A        raise CommandExecutionError(%22Parameter 'on' is missing and should be True or False%22)%0A%0A    out = dict()%0A    for dev_id in _get_devices(kwargs):%0A        out%5Bdev_id%5D = _set(dev_id, kwargs%5B'on'%5D and Const.LAMP_ON or Const.LAMP_OFF)%0A%0A    return out%0A%0A%0A
 def call_pin

Implement light switch ON/OFF