示例#1
0
    def __init__(self, driver, driver_lock, controller_name, ballast):
        from dali.gear.general import QueryActualLevel
        from dali.command import ResponseError, MissingResponse
        """Initialize a DALI Light."""
        self._brightness = 0
        self._state = False       
        self._name = "{}_{}".format(controller_name, ballast.address)
        self.attributes = {"short_address": ballast.address}
        
        self.driver = driver
        self.driver_lock = driver_lock
        self.addr = ballast

        try:
            with self.driver_lock:
                cmd = QueryActualLevel(self.addr)
                r = self.driver.send(cmd)
                if r.value != None and r.value.as_integer < 255:
                    self._brightness = r.value.as_integer
                    if r.value.as_integer > 0:
                        r.state = True

        except ResponseError as e:
            _LOGGER.error("Response error on __init__")
        except MissingResponse as e:
            self._brightness = None
            self._state = None
    def __init__(self, driver, driver_lock, controller_name, ballast,
                 bus_index):
        from dali.gear.general import QueryActualLevel
        from dali.command import ResponseError, MissingResponse
        """Initialize a DALI light."""
        self._brightness = 0
        self._state = False
        self._name = "{}_{}".format(controller_name, ballast.address)
        self.attributes = {"short_address": ballast.address}

        self.driver = driver
        self.driver_lock = driver_lock
        self.addr = ballast

        # A range of MAX_RANGE unique lamp IDs is allocated for each bus
        self._unique_id = (MAX_RANGE * bus_index) + self.addr.address

        try:
            with self.driver_lock:
                cmd = QueryActualLevel(self.addr)
                r = self.driver.send(cmd)
                if r.value != None and r.value < 255:
                    self._brightness = r.value
                    if r.value > 0:
                        r.state = True

        except ResponseError as e:
            _LOGGER.error("Response error on __init__")
        except MissingResponse as e:
            self._brightness = None
            self._state = None
    def update(self):
        """Fetch update state."""
        from dali.gear.general import QueryActualLevel
        from dali.command import ResponseError, MissingResponse
        import usb

        with self.driver_lock:
            try:
                r = self.driver.send(QueryActualLevel(self.addr))
                _LOGGER.debug(
                    "DALI Light update: new brightness is {}".format(r))
                if r:
                    self._brightness = r.value
                    if 0 < int(self._brightness) < 255:
                        self._state = True
                    else:
                        self._state = False
                else:
                    _LOGGER.error("return value = {}", r)
            except usb.core.USBError as e:
                _LOGGER.error("Can't update {}: {}".format(self._name, e))
            except ResponseError as e:
                _LOGGER.error("ResponseError QueryActualLevel")
            except MissingResponse as e:
                self._brightness = None
    def calculate_bus_state(self):
        from dali.gear.general import QueryActualLevel
        from dali.command import ResponseError, MissingResponse
        import usb
        """The state of a DALI bus will be the same state as the lights hanging 
        from it if all of them are consistent. If they are not, the state of the 
        bus will be off. """

        try:
            with self.driver_lock:
                last_brightness = None

                for lamp_address in self.lamp_addresses:
                    # Query brightness
                    result = self.driver.send(QueryActualLevel(lamp_address))
                    _LOGGER.debug(
                        "DALI Bus update: lamp {} brightness is {}".format(
                            lamp_address.address, result.value))

                    # Check if brightness is a valid value
                    # If so, and if it's either the first light or if it has the same value as the lights
                    # checked before, save value and keep checking
                    if result.value != None and int(result.value) < 255 and (
                            last_brightness == None
                            or last_brightness == int(result.value)):
                        last_brightness = int(result.value)

                    else:  # if not, bus status is not consistent; stop checking
                        _LOGGER.debug(
                            "Lamp {} returned invalid or different value; bus status is not consistent"
                        )
                        last_brightness = None
                        break

            _LOGGER.debug("DALI Bus update: new brightness is {}".format(
                last_brightness))

            self._brightness = last_brightness
            if self._brightness == None:
                self._state = None
            elif self._brightness > 0:
                self._state = True
            else:
                self._state = False

        # @XXX should unify these if I can't different causes / behavior
        except usb.core.USBError as e:
            _LOGGER.error("USB Error {}: {}".format(self._name, e))
            self._brightness = None
            self._state = None
        except ResponseError as e:
            _LOGGER.error("Response Error in QueryActualLevel: {}: {}".format(
                self._name, e))
            self._brightness = None
            self._state = None
        except MissingResponse as e:
            _LOGGER.error("Missing response: {}: {}".format(self._name, e))
            self._brightness = None
            self._state = None
示例#5
0
async def main(loop):
    # Edit to pick a device type.
    dev = tridonic("/dev/dali/daliusb-*", glob=True, loop=loop)
    #dev = hasseb("/dev/dali/hasseb-*", glob=True, loop=loop)
    dev.connect()
    print("Waiting to be connected...")
    await dev.connected.wait()
    print(f"Connected, firmware={dev.firmware_version}, serial={dev.serial}")
    for i in range(3):
        print("Set max...")
        await dev.send(RecallMaxLevel(Broadcast()))
        await asyncio.sleep(1)
        response = await dev.send(QueryActualLevel(Broadcast()))
        print(f"Response was {response}")
        print("Set min...")
        await dev.send(RecallMinLevel(Broadcast()))
        await asyncio.sleep(1)
        response = await dev.send(QueryActualLevel(Broadcast()))
        print(f"Response was {response}")
    await dev.send(Off(Broadcast()))
    dev.disconnect()
示例#6
0
    def update(self):
        """Fetch update state."""
        from dali.gear.general import QueryActualLevel
        from dali.gear.general import QueryLampPowerOn

        try:
            with self.driver_lock:
                cmd = QueryLampPowerOn(self.addr)
                r = self.driver.send(cmd)
                self._state = bool(r.value)
                _LOGGER.debug("{} is_on ? {}".format(self._id, self._state))
        except usb.core.USBError as e:
            _LOGGER.warning(e)

        if not self._state:
            return
        with self.driver_lock:
            cmd = QueryActualLevel(self.addr)
            r = self.driver.send(cmd)
            self._brightness = to_hass_level(r.value.as_integer)

        _LOGGER.debug("[{}] brightness = {} ".format(self._name,
                                                     self._brightness))
示例#7
0
    import sys
    import time

    # setup console logging
    logger = logging.getLogger('UnipiDALIDriver')
    #logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(
        logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    logger.addHandler(handler)

    # sync interface
    addr = Short(int(sys.argv[1]))
    # command to send
    #command = DAPC(addr, int(sys.argv[2]))
    #_test_sync(logger, command)
    command = QueryStatus(addr)
    _test_sync(logger, command)
    command = QueryDeviceType(addr)
    _test_sync(logger, command)
    #command = QueryLightSourceType(addr)
    #_test_sync(logger, command)
    command = QueryActualLevel(addr)
    _test_sync(logger, command)
    #command = RecallMaxLevel(addr)
    #command = RecallMinLevel(addr)
    command = Off(addr)
    _test_sync(logger, command)