# Path: coherence/upnp/core/utils.py
# def generalise_boolean(value):
#     """ standardize the different boolean incarnations
# 
#         transform anything that looks like a "True" into a '1',
#         and everything else into a '0'
#     """
#     if means_true(value):
#         return '1'
#     return '0'
# 
# Path: coherence/backend.py
# class Backend(log.Loggable, Plugin):
# 
#     """ the base class for all backends
# 
#         if there are any UPnP service actions, that can't
#         be handled by the service classes itself, or need some
#         special adjustments for the backend, they need to be
#         defined here.
# 
#         Like maybe upnp_Browse for the CDS Browse action.
#     """
# 
#     implements = []  # list the device classes here
#                      # like [BinaryLight'] or ['MediaServer','MediaRenderer']
# 
#     logCategory = 'backend'
# 
#     def __init__(self, server, **kwargs):
#         """ the init method for a backend,
#             should probably most of the time be overwritten
#             when the init is done, send a signal to its device
# 
#             the device will then setup and announce itself,
#             after that it calls the backends upnp_init method
#         """
#         self.config = kwargs
#         self.server = server  # the UPnP device that's hosting that backend
# 
#         """ do whatever is necessary with the stuff we can
#             extract from the config dict,
#             connect maybe to an external data-source and
#             start up the backend
#             after that's done, tell Coherence about it
#         """
#         log.Loggable.__init__(self)
#         Plugin.__init__(self)
# 
#         """ this has to be done in the actual backend, maybe it has
#             to wait for an answer from an external data-source first
#         """
#         #self.init_completed()
# 
#     def init_completed(self, *args, **kwargs):
#         """ inform Coherence that this backend is ready for
#             announcement
#             this method just accepts any form of arguments
#             as we don't under which circumstances it is called
#         """
#         louie.send('Coherence.UPnP.Backend.init_completed',
#                 None, backend=self)
# 
#     def upnp_init(self):
#         """ this method gets called after the device is fired,
#             here all initializations of service related state variables
#             should happen, as the services aren't available before that point
#         """
#         pass


import coherence.extern.louie as louie
from coherence.upnp.core.utils import generalise_boolean
from coherence.backend import Backend
    from coherence.base import Coherence
    from twisted.internet import reactor
        if self.server:
            self.server.switch_power_server.set_variable(0, 'Target', self.state)
            self.server.switch_power_server.set_variable(0, 'Status', self.state)
        print "we have been switched to state", self.state
        return {}


class BetterLight(Backend):

    implements = ['DimmableLight']
    logCategory = 'better_light'

    def __init__(self, server, **kwargs):
        Backend.__init__(self, server)
        self.name = kwargs.get('name', DEFAULT_NAME_BETTER)
        self.server = server
        self.state = 0  # we start switched off
        self.loadlevel = 50  # we start with 50% brightness

        louie.send('Coherence.UPnP.Backend.init_completed', None, backend=self)

    def upnp_init(self):
        if self.server:
            self.server.switch_power_server.set_variable(0, 'Target', self.state)
            self.server.switch_power_server.set_variable(0, 'Status', self.state)
            self.server.dimming_server.set_variable(0, 'LoadLevelTarget', self.loadlevel)
            self.server.dimming_server.set_variable(0, 'LoadLevelStatus', self.loadlevel)

    def upnp_SetTarget(self, **kwargs):
        self.info('upnp_SetTarget %r', kwargs)

# Path: coherence/upnp/core/utils.py
# def generalise_boolean(value):
#     """ standardize the different boolean incarnations
# 
#         transform anything that looks like a "True" into a '1',
#         and everything else into a '0'
#     """
#     if means_true(value):
#         return '1'
#     return '0'
# 
# Path: coherence/backend.py
# class Backend(log.Loggable, Plugin):
# 
#     """ the base class for all backends
# 
#         if there are any UPnP service actions, that can't
#         be handled by the service classes itself, or need some
#         special adjustments for the backend, they need to be
#         defined here.
# 
#         Like maybe upnp_Browse for the CDS Browse action.
#     """
# 
#     implements = []  # list the device classes here
#                      # like [BinaryLight'] or ['MediaServer','MediaRenderer']
# 
#     logCategory = 'backend'
# 
#     def __init__(self, server, **kwargs):
#         """ the init method for a backend,
#             should probably most of the time be overwritten
#             when the init is done, send a signal to its device
# 
#             the device will then setup and announce itself,
#             after that it calls the backends upnp_init method
#         """
#         self.config = kwargs
#         self.server = server  # the UPnP device that's hosting that backend
# 
#         """ do whatever is necessary with the stuff we can
#             extract from the config dict,
#             connect maybe to an external data-source and
#             start up the backend
#             after that's done, tell Coherence about it
#         """
#         log.Loggable.__init__(self)
#         Plugin.__init__(self)
# 
#         """ this has to be done in the actual backend, maybe it has
#             to wait for an answer from an external data-source first
#         """
#         #self.init_completed()
# 
#     def init_completed(self, *args, **kwargs):
#         """ inform Coherence that this backend is ready for
#             announcement
#             this method just accepts any form of arguments
#             as we don't under which circumstances it is called
#         """
#         louie.send('Coherence.UPnP.Backend.init_completed',
#                 None, backend=self)
# 
#     def upnp_init(self):
#         """ this method gets called after the device is fired,
#             here all initializations of service related state variables
#             should happen, as the services aren't available before that point
#         """
#         pass


# Path: coherence/backends/light.py
import coherence.extern.louie as louie
from coherence.upnp.core.utils import generalise_boolean
from coherence.backend import Backend
    from coherence.base import Coherence
    from twisted.internet import reactor

        if self.server:
            self.server.switch_power_server.set_variable(0, 'Target', self.state)
            self.server.switch_power_server.set_variable(0, 'Status', self.state)
        print "we have been switched to state", self.state
        return {}


class BetterLight(Backend):

    implements = ['DimmableLight']
    logCategory = 'better_light'

    def __init__(self, server, **kwargs):
        Backend.__init__(self, server)
        self.name = kwargs.get('name', DEFAULT_NAME_BETTER)
        self.server = server
        self.state = 0  # we start switched off
        self.loadlevel = 50  # we start with 50% brightness

        louie.send('Coherence.UPnP.Backend.init_completed', None, backend=self)

    def upnp_init(self):
        if self.server:
            self.server.switch_power_server.set_variable(0, 'Target', self.state)
            self.server.switch_power_server.set_variable(0, 'Status', self.state)
            self.server.dimming_server.set_variable(0, 'LoadLevelTarget', self.loadlevel)
            self.server.dimming_server.set_variable(0, 'LoadLevelStatus', self.loadlevel)

    def upnp_SetTarget(self, **kwargs):
        self.info('upnp_SetTarget %r', kwargs)

        self.state = int(generalise_boolean(kwargs['NewTargetValue']))