Ejemplo n.º 1
0
    def __init__(self, hass, config, required, optional=None):
        """Initialize the device.

        The namelist argument lists the required addresses. E.g. for a dimming
        actuators, the namelist might look like:
        onoff_address: 0/0/1
        brightness_address: 0/0/2
        """
        from knxip.core import parse_group_address, KNXException

        self.names = {}
        self.values = {}

        self._config = config
        self._state = False
        self._data = None
        _LOGGER.debug("%s: initalizing KNX multi address device", self.name)

        settings = self._config.config
        if config.address:
            _LOGGER.debug("%s: base address: address=%s", self.name,
                          settings.get('address'))
            self.names[config.address] = 'base'
        if config.state_address:
            _LOGGER.debug("%s, state address: state_address=%s", self.name,
                          settings.get('state_address'))
            self.names[config.state_address] = 'state'

        # parse required addresses
        for name in required:
            paramname = '{}{}'.format(name, '_address')
            addr = settings.get(paramname)
            if addr is None:
                _LOGGER.error("%s: Required KNX group address %s missing",
                              self.name, paramname)
                raise KNXException("%s: Group address for {} missing in "
                                   "configuration for {}".format(
                                       self.name, paramname))
            _LOGGER.debug("%s: (required parameter) %s=%s", self.name,
                          paramname, addr)
            addr = parse_group_address(addr)
            self.names[addr] = name

        # parse optional addresses
        for name in optional:
            paramname = '{}{}'.format(name, '_address')
            addr = settings.get(paramname)
            _LOGGER.debug("%s: (optional parameter) %s=%s", self.name,
                          paramname, addr)
            if addr:
                try:
                    addr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.exception("%s: cannot parse group address %s",
                                      self.name, addr)
                self.names[addr] = name
Ejemplo n.º 2
0
    def __init__(self, config):
        """Initialize the configuration."""
        from knxip.core import parse_group_address

        self.config = config
        self.should_poll = config.get("poll", True)
        self._address = parse_group_address(config.get("address"))
        if self.config.get("state_address"):
            self._state_address = parse_group_address(
                self.config.get("state_address"))
        else:
            self._state_address = None
Ejemplo n.º 3
0
    def __init__(self, hass, config, required, optional=None):
        """Initialize the device.

        The namelist argument lists the required addresses. E.g. for a dimming
        actuators, the namelist might look like:
        onoff_address: 0/0/1
        brightness_address: 0/0/2
        """
        from knxip.core import parse_group_address, KNXException

        super().__init__(self, hass, config)

        self.config = config

        # parse required addresses
        for name in required:
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr is None:
                _LOGGER.exception("Required KNX group address %s missing",
                                  paramname)
                raise KNXException("Group address missing in configuration")
            addr = parse_group_address(addr)
            self.names[addr] = name

        # parse optional addresses
        for name in optional:
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr:
                try:
                    addr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.exception("Cannot parse group address %s", addr)
                self.names[addr] = name

        def handle_frame(frame):
            """Handle an incoming KNX frame.

            Handle an incoming frame and update our status if it contains
            information relating to this device.
            """
            addr = frame.data[0]

            if addr in self.names:
                self.values[addr] = frame.data[1]
                self.update_ha_state()

        hass.bus.listen(EVENT_KNX_FRAME_RECEIVED, handle_frame)
Ejemplo n.º 4
0
    def __init__(self, config):
        """Initialize the configuration."""
        from knxip.core import parse_group_address

        self.config = config
        self.should_poll = config.get('poll', True)
        if config.get('address'):
            self._address = parse_group_address(config.get('address'))
        else:
            self._address = None
        if self.config.get('state_address'):
            self._state_address = parse_group_address(
                self.config.get('state_address'))
        else:
            self._state_address = None
Ejemplo n.º 5
0
 def test_group_address(self):
     """Does the group address parser work correctly?"""
     self.assertEquals(parse_group_address("1"), 1)
     self.assertEquals(parse_group_address("1678"), 1678)
     self.assertEquals(parse_group_address("1/1"), 2049)
     self.assertEquals(parse_group_address("2/2"), 4098)
     self.assertEquals(parse_group_address("0/0/1"), 1)
     self.assertEquals(parse_group_address("1/1/1"), 2305)
     self.assertEquals(parse_group_address("4/8/45"), 10285)
Ejemplo n.º 6
0
 def test_group_address(self):
     """Does the group address parser work correctly?"""
     self.assertEquals(parse_group_address("1"), 1)
     self.assertEquals(parse_group_address("1678"), 1678)
     self.assertEquals(parse_group_address("1/1"), 2049)
     self.assertEquals(parse_group_address("2/2"), 4098)
     self.assertEquals(parse_group_address("0/0/1"), 1)
     self.assertEquals(parse_group_address("1/1/1"), 2305)
     self.assertEquals(parse_group_address("4/8/45"), 10285)
Ejemplo n.º 7
0
    def __init__(self, hass, config, required, optional=None):
        """Initialize the device.

        The namelist argument lists the required addresses. E.g. for a dimming
        actuators, the namelist might look like:
        onoff_address: 0/0/1
        brightness_address: 0/0/2
        """
        from knxip.core import parse_group_address, KNXException

        self._config = config
        self._state = False
        self._data = None
        _LOGGER.debug("Initalizing KNX multi address device")

        # parse required addresses
        for name in required:
            _LOGGER.info(name)
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr is None:
                _LOGGER.exception("Required KNX group address %s missing",
                                  paramname)
                raise KNXException(
                    "Group address for %s missing "
                    "in configuration", paramname)
            addr = parse_group_address(addr)
            self.names[addr] = name

        # parse optional addresses
        for name in optional:
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr:
                try:
                    addr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.exception("Cannot parse group address %s", addr)
                self.names[addr] = name
Ejemplo n.º 8
0
    def __init__(self, hass, config, required, optional=None):
        """Initialize the device.

        The namelist argument lists the required addresses. E.g. for a dimming
        actuators, the namelist might look like:
        onoff_address: 0/0/1
        brightness_address: 0/0/2
        """
        from knxip.core import parse_group_address, KNXException

        self._config = config
        self._state = False
        self._data = None
        _LOGGER.debug("Initalizing KNX multi address device")

        # parse required addresses
        for name in required:
            _LOGGER.info(name)
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr is None:
                _LOGGER.exception("Required KNX group address %s missing",
                                  paramname)
                raise KNXException("Group address for %s missing "
                                   "in configuration", paramname)
            addr = parse_group_address(addr)
            self.names[addr] = name

        # parse optional addresses
        for name in optional:
            paramname = name + "_address"
            addr = self._config.config.get(paramname)
            if addr:
                try:
                    addr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.exception("Cannot parse group address %s", addr)
                self.names[addr] = name
Ejemplo n.º 9
0
    def __init__(self, knxiptunnel,
                 dateaddr=None,
                 timeaddr=None,
                 datetimeaddr=None,
                 daynightaddr=None,
                 lat=45,
                 long=0,
                 updateinterval=60):
        """ Initialize the updater. """

        self.tunnel = knxiptunnel
        self.dateaddr = dateaddr

        if timeaddr is not None:
            self.timeaddr = parse_group_address(timeaddr)
        else:
            self.timeaddr = None

        if dateaddr is not None:
            self.dateaddr = parse_group_address(dateaddr)
        else:
            self.dateaddr = None

        if datetimeaddr is not None:
            self.datetimeaddr = parse_group_address(datetimeaddr)
        else:
            self.datetimeaddr = None

        if daynightaddr is not None:
            self.daynightaddr = parse_group_address(daynightaddr)
        else:
            self.daynightaddr = None

        self.updateinterval = updateinterval
        self.lat = lat
        self.long = long
        self.updater_running = False
Ejemplo n.º 10
0
    def _handle_new_archive_record(self, event):
        record = event.record
        tunnel = self._knx_tunnel

        try:
            res = tunnel.connect()
            if not res:
                logerr(
                    'Could not connect to KNX/IP interface {0}, retry later'.
                    format(self._gateway_ip))
                return
        except KNXException as ex:
            logerr(
                'Exception during connect to KNX/IP interface {0}: {1}, retry later'
                .format(self._gateway_ip, ex))
            return

        try:
            for key, value in self._knx_map.items():
                data = record.get(key)
                if data is not None:
                    logdbg('Send {0} with data {1} to address {2}'.format(
                        key, data, value))
                    encoded_data = float_to_knx2(float(data))
                    tunnel.group_write(parse_group_address(value),
                                       encoded_data)
                else:
                    logerr(
                        'No value available for {0} and address {1}, nothing send'
                        .format(key, value))

        except KNXException as ex:
            logerr('KNXException raised : {0}'.format(ex))

        try:
            tunnel.disconnect()
        except KNXException as ex:
            logerr('KNXException raised : {0}'.format(ex))
Ejemplo n.º 11
0
    def handle_knx_send(event):
        """Bridge knx_frame_send events to the KNX bus."""
        try:
            addr = event.data["address"]
        except KeyError:
            _LOGGER.error("KNX group address is missing")
            return

        try:
            data = event.data["data"]
        except KeyError:
            _LOGGER.error("KNX data block missing")
            return

        knxaddr = None
        try:
            addr = int(addr)
        except ValueError:
            pass

        if knxaddr is None:
            try:
                knxaddr = parse_group_address(addr)
            except KNXException:
                _LOGGER.error("KNX address format incorrect")
                return

        knxdata = None
        if isinstance(data, list):
            knxdata = data
        else:
            try:
                knxdata = [int(data) & 0xff]
            except ValueError:
                _LOGGER.error("KNX data format incorrect")
                return

        KNXTUNNEL.group_write(knxaddr, knxdata)
Ejemplo n.º 12
0
    def handle_knx_send(event):
        """Bridge knx_frame_send events to the KNX bus."""
        try:
            addr = event.data["address"]
        except KeyError:
            _LOGGER.error("KNX group address is missing")
            return

        try:
            data = event.data["data"]
        except KeyError:
            _LOGGER.error("KNX data block missing")
            return

        knxaddr = None
        try:
            addr = int(addr)
        except ValueError:
            pass

        if knxaddr is None:
            try:
                knxaddr = parse_group_address(addr)
            except KNXException:
                _LOGGER.error("KNX address format incorrect")
                return

        knxdata = None
        if isinstance(data, list):
            knxdata = data
        else:
            try:
                knxdata = [int(data) & 0xff]
            except ValueError:
                _LOGGER.error("KNX data format incorrect")
                return

        KNXTUNNEL.group_write(knxaddr, knxdata)
Ejemplo n.º 13
0
    def handle_group_write(call):
        """Bridge knx_frame_send events to the KNX bus."""
        # parameters are pre-validated using KNX_WRITE_SCHEMA
        addrlist = call.data.get("address")
        knxdata = call.data.get("data")

        knxaddrlist = []
        for addr in addrlist:
            try:
                _LOGGER.debug("Found %s", addr)
                knxaddr = int(addr)
            except ValueError:
                knxaddr = None

            if knxaddr is None:
                try:
                    knxaddr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.error("KNX address format incorrect: %s", addr)

            knxaddrlist.append(knxaddr)

        for addr in knxaddrlist:
            KNXTUNNEL.group_write(addr, knxdata)
Ejemplo n.º 14
0
def setup(hass, config):
    """Set up the connection to the KNX IP interface."""
    global KNXTUNNEL

    from knxip.ip import KNXIPTunnel
    from knxip.core import KNXException, parse_group_address

    host = config[DOMAIN].get(CONF_HOST)
    port = config[DOMAIN].get(CONF_PORT)

    if host == '0.0.0.0':
        _LOGGER.debug("Will try to auto-detect KNX/IP gateway")

    KNXTUNNEL = KNXIPTunnel(host, port)
    try:
        res = KNXTUNNEL.connect()
        _LOGGER.debug("Res = %s", res)
        if not res:
            _LOGGER.error("Could not connect to KNX/IP interface %s", host)
            return False

    except KNXException as ex:
        _LOGGER.exception("Can't connect to KNX/IP interface: %s", ex)
        KNXTUNNEL = None
        return False

    _LOGGER.info("KNX IP tunnel to %s:%i established", host, port)

    def received_knx_event(address, data):
        """Process received KNX message."""
        if len(data) == 1:
            data = data[0]
        hass.bus.fire('knx_event', {
            'address': address,
            'data': data
        })

    for listen in config[DOMAIN].get(CONF_LISTEN):
        _LOGGER.debug("Registering listener for %s", listen)
        try:
            KNXTUNNEL.register_listener(parse_group_address(listen),
                                        received_knx_event)
        except KNXException as knxexception:
            _LOGGER.error("Can't register KNX listener for address %s (%s)",
                          listen, knxexception)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_tunnel)

    # Listen to KNX events and send them to the bus
    def handle_knx_send(event):
        """Bridge knx_frame_send events to the KNX bus."""
        try:
            addr = event.data["address"]
        except KeyError:
            _LOGGER.error("KNX group address is missing")
            return

        try:
            data = event.data["data"]
        except KeyError:
            _LOGGER.error("KNX data block missing")
            return

        knxaddr = None
        try:
            addr = int(addr)
        except ValueError:
            pass

        if knxaddr is None:
            try:
                knxaddr = parse_group_address(addr)
            except KNXException:
                _LOGGER.error("KNX address format incorrect")
                return

        knxdata = None
        if isinstance(data, list):
            knxdata = data
        else:
            try:
                knxdata = [int(data) & 0xff]
            except ValueError:
                _LOGGER.error("KNX data format incorrect")
                return

        KNXTUNNEL.group_write(knxaddr, knxdata)

    # Listen for when knx_frame_send event is fired
    hass.bus.listen(EVENT_KNX_FRAME_SEND, handle_knx_send)

    return True
Ejemplo n.º 15
0
def setup(hass, config):
    """Set up the connection to the KNX IP interface."""
    global KNXTUNNEL

    from knxip.ip import KNXIPTunnel
    from knxip.core import KNXException, parse_group_address

    host = config[DOMAIN].get(CONF_HOST)
    port = config[DOMAIN].get(CONF_PORT)

    if host == '0.0.0.0':
        _LOGGER.debug("Will try to auto-detect KNX/IP gateway")

    KNXTUNNEL = KNXIPTunnel(host, port)
    try:
        res = KNXTUNNEL.connect()
        _LOGGER.debug("Res = %s", res)
        if not res:
            _LOGGER.error("Could not connect to KNX/IP interface %s", host)
            return False

    except KNXException as ex:
        _LOGGER.exception("Can't connect to KNX/IP interface: %s", ex)
        KNXTUNNEL = None
        return False

    _LOGGER.info("KNX IP tunnel to %s:%i established", host, port)

    def received_knx_event(address, data):
        """Process received KNX message."""
        if len(data) == 1:
            data = data[0]
        hass.bus.fire('knx_event', {'address': address, 'data': data})

    for listen in config[DOMAIN].get(CONF_LISTEN):
        _LOGGER.debug("Registering listener for %s", listen)
        try:
            KNXTUNNEL.register_listener(parse_group_address(listen),
                                        received_knx_event)
        except KNXException as knxexception:
            _LOGGER.error("Can't register KNX listener for address %s (%s)",
                          listen, knxexception)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_tunnel)

    # Listen to KNX events and send them to the bus
    def handle_knx_send(event):
        """Bridge knx_frame_send events to the KNX bus."""
        try:
            addr = event.data["address"]
        except KeyError:
            _LOGGER.error("KNX group address is missing")
            return

        try:
            data = event.data["data"]
        except KeyError:
            _LOGGER.error("KNX data block missing")
            return

        knxaddr = None
        try:
            addr = int(addr)
        except ValueError:
            pass

        if knxaddr is None:
            try:
                knxaddr = parse_group_address(addr)
            except KNXException:
                _LOGGER.error("KNX address format incorrect")
                return

        knxdata = None
        if isinstance(data, list):
            knxdata = data
        else:
            try:
                knxdata = [int(data) & 0xff]
            except ValueError:
                _LOGGER.error("KNX data format incorrect")
                return

        KNXTUNNEL.group_write(knxaddr, knxdata)

    # Listen for when knx_frame_send event is fired
    hass.bus.listen(EVENT_KNX_FRAME_SEND, handle_knx_send)

    return True
Ejemplo n.º 16
0
def setup(hass, config):
    """Set up the connection to the KNX IP interface."""
    global KNXTUNNEL

    from knxip.ip import KNXIPTunnel
    from knxip.core import KNXException, parse_group_address

    host = config[DOMAIN].get(CONF_HOST)
    port = config[DOMAIN].get(CONF_PORT)

    if host == '0.0.0.0':
        _LOGGER.debug("Will try to auto-detect KNX/IP gateway")

    KNXTUNNEL = KNXIPTunnel(host, port)
    try:
        res = KNXTUNNEL.connect()
        _LOGGER.debug("Res = %s", res)
        if not res:
            _LOGGER.error("Could not connect to KNX/IP interface %s", host)
            return False

    except KNXException as ex:
        _LOGGER.exception("Can't connect to KNX/IP interface: %s", ex)
        KNXTUNNEL = None
        return False

    _LOGGER.info("KNX IP tunnel to %s:%i established", host, port)

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    def received_knx_event(address, data):
        """Process received KNX message."""
        if len(data) == 1:
            data = data[0]
        hass.bus.fire('knx_event', {
            'address': address,
            'data': data
        })

    for listen in config[DOMAIN].get(CONF_LISTEN):
        _LOGGER.debug("Registering listener for %s", listen)
        try:
            KNXTUNNEL.register_listener(parse_group_address(listen),
                                        received_knx_event)
        except KNXException as knxexception:
            _LOGGER.error("Can't register KNX listener for address %s (%s)",
                          listen, knxexception)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_tunnel)

    # Listen to KNX events and send them to the bus
    def handle_group_write(call):
        """Bridge knx_frame_send events to the KNX bus."""
        # parameters are pre-validated using KNX_WRITE_SCHEMA
        addrlist = call.data.get("address")
        knxdata = call.data.get("data")

        knxaddrlist = []
        for addr in addrlist:
            try:
                _LOGGER.debug("Found %s", addr)
                knxaddr = int(addr)
            except ValueError:
                knxaddr = None

            if knxaddr is None:
                try:
                    knxaddr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.error("KNX address format incorrect: %s", addr)

            knxaddrlist.append(knxaddr)

        for addr in knxaddrlist:
            KNXTUNNEL.group_write(addr, knxdata)

    # Listen for when knx_frame_send event is fired
    hass.services.register(DOMAIN,
                           KNX_GROUP_WRITE,
                           handle_group_write,
                           descriptions[DOMAIN][KNX_GROUP_WRITE],
                           schema=KNX_WRITE_SCHEMA)

    return True
Ejemplo n.º 17
0
    def __init__(self, hass, config, required, optional=None):
        """Initialize the device.

        The namelist argument lists the required addresses. E.g. for a dimming
        actuators, the namelist might look like:
        onoff_address: 0/0/1
        brightness_address: 0/0/2
        """
        from knxip.core import parse_group_address, KNXException

        self.names = {}
        self.values = {}

        self._config = config
        self._state = False
        self._data = None
        _LOGGER.debug(
            "%s: initalizing KNX multi address device",
            self.name
        )

        settings = self._config.config
        if config.address:
            _LOGGER.debug(
                "%s: base address: address=%s",
                self.name, settings.get('address')
            )
            self.names[config.address] = 'base'
        if config.state_address:
            _LOGGER.debug(
                "%s, state address: state_address=%s",
                self.name, settings.get('state_address')
            )
            self.names[config.state_address] = 'state'

        # parse required addresses
        for name in required:
            paramname = '{}{}'.format(name, '_address')
            addr = settings.get(paramname)
            if addr is None:
                _LOGGER.error(
                    "%s: Required KNX group address %s missing",
                    self.name, paramname
                )
                raise KNXException(
                    "%s: Group address for {} missing in "
                    "configuration for {}".format(
                        self.name, paramname
                    )
                )
            _LOGGER.debug(
                "%s: (required parameter) %s=%s",
                self.name, paramname, addr
            )
            addr = parse_group_address(addr)
            self.names[addr] = name

        # parse optional addresses
        for name in optional:
            paramname = '{}{}'.format(name, '_address')
            addr = settings.get(paramname)
            _LOGGER.debug(
                "%s: (optional parameter) %s=%s",
                self.name, paramname, addr
            )
            if addr:
                try:
                    addr = parse_group_address(addr)
                except KNXException:
                    _LOGGER.exception(
                        "%s: cannot parse group address %s",
                        self.name, addr
                    )
                self.names[addr] = name