Exemplo n.º 1
0
    async def setup_gateway(
            device, persistence_file, baud_rate, tcp_port, in_prefix,
            out_prefix):
        """Return gateway after setup of the gateway."""
        if device == MQTT_COMPONENT:
            if not await async_setup_component(hass, MQTT_COMPONENT, config):
                return None
            mqtt = hass.components.mqtt
            retain = config[DOMAIN].get(CONF_RETAIN)

            def pub_callback(topic, payload, qos, retain):
                """Call MQTT publish function."""
                mqtt.async_publish(topic, payload, qos, retain)

            def sub_callback(topic, sub_cb, qos):
                """Call MQTT subscribe function."""
                @callback
                def internal_callback(*args):
                    """Call callback."""
                    sub_cb(*args)

                hass.async_add_job(
                    mqtt.async_subscribe(topic, internal_callback, qos))

            gateway = mysensors.AsyncMQTTGateway(
                pub_callback, sub_callback, in_prefix=in_prefix,
                out_prefix=out_prefix, retain=retain, loop=hass.loop,
                event_callback=None, persistence=persistence,
                persistence_file=persistence_file,
                protocol_version=version)
        else:
            try:
                await hass.async_add_job(is_serial_port, device)
                gateway = mysensors.AsyncSerialGateway(
                    device, baud=baud_rate, loop=hass.loop,
                    event_callback=None, persistence=persistence,
                    persistence_file=persistence_file,
                    protocol_version=version)
            except vol.Invalid:
                gateway = mysensors.AsyncTCPGateway(
                    device, port=tcp_port, loop=hass.loop, event_callback=None,
                    persistence=persistence, persistence_file=persistence_file,
                    protocol_version=version)
        gateway.metric = hass.config.units.is_metric
        gateway.optimistic = config[DOMAIN].get(CONF_OPTIMISTIC)
        gateway.device = device
        gateway.event_callback = gw_callback_factory(hass)
        if persistence:
            await gateway.start_persistence()

        return gateway
Exemplo n.º 2
0
async def _get_gateway(
    hass: HomeAssistant,
    gateway_type: ConfGatewayType,
    device: str,
    version: str,
    event_callback: Callable[[Message], None],
    persistence_file: str | None = None,
    baud_rate: int | None = None,
    tcp_port: int | None = None,
    topic_in_prefix: str | None = None,
    topic_out_prefix: str | None = None,
    retain: bool = False,
    persistence: bool = True,
) -> BaseAsyncGateway | None:
    """Return gateway after setup of the gateway."""

    if persistence_file is not None:
        # Interpret relative paths to be in hass config folder.
        # Absolute paths will be left as they are.
        persistence_file = hass.config.path(persistence_file)

    if gateway_type == CONF_GATEWAY_TYPE_MQTT:
        # Make sure the mqtt integration is set up.
        # Naive check that doesn't consider config entry state.
        if MQTT_DOMAIN not in hass.config.components:
            return None
        mqtt = hass.components.mqtt

        def pub_callback(topic: str, payload: str, qos: int,
                         retain: bool) -> None:
            """Call MQTT publish function."""
            hass.async_create_task(
                mqtt.async_publish(hass, topic, payload, qos, retain))

        def sub_callback(topic: str,
                         sub_cb: Callable[[str, ReceivePayloadType, int],
                                          None], qos: int) -> None:
            """Call MQTT subscribe function."""
            @callback
            def internal_callback(msg: MQTTReceiveMessage) -> None:
                """Call callback."""
                sub_cb(msg.topic, msg.payload, msg.qos)

            hass.async_create_task(
                mqtt.async_subscribe(topic, internal_callback, qos))

        gateway = mysensors.AsyncMQTTGateway(
            pub_callback,
            sub_callback,
            in_prefix=topic_in_prefix,
            out_prefix=topic_out_prefix,
            retain=retain,
            loop=hass.loop,
            event_callback=None,
            persistence=persistence,
            persistence_file=persistence_file,
            protocol_version=version,
        )
    elif gateway_type == CONF_GATEWAY_TYPE_SERIAL:
        gateway = mysensors.AsyncSerialGateway(
            device,
            baud=baud_rate,
            loop=hass.loop,
            event_callback=None,
            persistence=persistence,
            persistence_file=persistence_file,
            protocol_version=version,
        )
    else:
        gateway = mysensors.AsyncTCPGateway(
            device,
            port=tcp_port,
            loop=hass.loop,
            event_callback=None,
            persistence=persistence,
            persistence_file=persistence_file,
            protocol_version=version,
        )
    gateway.event_callback = event_callback
    gateway.metric = hass.config.units.is_metric

    if persistence:
        await gateway.start_persistence()

    return gateway
Exemplo n.º 3
0
async def _get_gateway(hass, config, gateway_conf, persistence_file):
    """Return gateway after setup of the gateway."""
    from mysensors import mysensors

    conf = config[DOMAIN]
    persistence = conf[CONF_PERSISTENCE]
    version = conf[CONF_VERSION]
    device = gateway_conf[CONF_DEVICE]
    baud_rate = gateway_conf[CONF_BAUD_RATE]
    tcp_port = gateway_conf[CONF_TCP_PORT]
    in_prefix = gateway_conf.get(CONF_TOPIC_IN_PREFIX, '')
    out_prefix = gateway_conf.get(CONF_TOPIC_OUT_PREFIX, '')

    if device == MQTT_COMPONENT:
        if not await async_setup_component(hass, MQTT_COMPONENT, config):
            return None
        mqtt = hass.components.mqtt
        retain = conf[CONF_RETAIN]

        def pub_callback(topic, payload, qos, retain):
            """Call MQTT publish function."""
            mqtt.async_publish(topic, payload, qos, retain)

        def sub_callback(topic, sub_cb, qos):
            """Call MQTT subscribe function."""
            @callback
            def internal_callback(msg):
                """Call callback."""
                sub_cb(msg.topic, msg.payload, msg.qos)

            hass.async_create_task(
                mqtt.async_subscribe(topic, internal_callback, qos))

        gateway = mysensors.AsyncMQTTGateway(pub_callback,
                                             sub_callback,
                                             in_prefix=in_prefix,
                                             out_prefix=out_prefix,
                                             retain=retain,
                                             loop=hass.loop,
                                             event_callback=None,
                                             persistence=persistence,
                                             persistence_file=persistence_file,
                                             protocol_version=version)
    else:
        try:
            await hass.async_add_job(is_serial_port, device)
            gateway = mysensors.AsyncSerialGateway(
                device,
                baud=baud_rate,
                loop=hass.loop,
                event_callback=None,
                persistence=persistence,
                persistence_file=persistence_file,
                protocol_version=version)
        except vol.Invalid:
            try:
                await hass.async_add_job(is_socket_address, device)
                # valid ip address
                gateway = mysensors.AsyncTCPGateway(
                    device,
                    port=tcp_port,
                    loop=hass.loop,
                    event_callback=None,
                    persistence=persistence,
                    persistence_file=persistence_file,
                    protocol_version=version)
            except vol.Invalid:
                # invalid ip address
                return None
    gateway.metric = hass.config.units.is_metric
    gateway.optimistic = conf[CONF_OPTIMISTIC]
    gateway.device = device
    gateway.event_callback = _gw_callback_factory(hass, config)
    gateway.nodes_config = gateway_conf[CONF_NODES]
    if persistence:
        await gateway.start_persistence()

    return gateway
Exemplo n.º 4
0
async def _get_gateway(
    opp: OpenPeerPower,
    device: str,
    version: str,
    event_callback: Callable[[Message], None],
    persistence_file: str | None = None,
    baud_rate: int | None = None,
    tcp_port: int | None = None,
    topic_in_prefix: str | None = None,
    topic_out_prefix: str | None = None,
    retain: bool = False,
    persistence:
    bool = True,  # old persistence option has been deprecated. kwarg is here so we can run try_connect() without persistence
) -> BaseAsyncGateway | None:
    """Return gateway after setup of the gateway."""

    if persistence_file is not None:
        # interpret relative paths to be in opp config folder. absolute paths will be left as they are
        persistence_file = opp.config.path(persistence_file)

    if device == MQTT_COMPONENT:
        # Make sure the mqtt integration is set up.
        # Naive check that doesn't consider config entry state.
        if MQTT_DOMAIN not in opp.config.components:
            return None
        mqtt = opp.components.mqtt

        def pub_callback(topic, payload, qos, retain):
            """Call MQTT publish function."""
            mqtt.async_publish(topic, payload, qos, retain)

        def sub_callback(topic, sub_cb, qos):
            """Call MQTT subscribe function."""
            @callback
            def internal_callback(msg):
                """Call callback."""
                sub_cb(msg.topic, msg.payload, msg.qos)

            opp.async_create_task(
                mqtt.async_subscribe(topic, internal_callback, qos))

        gateway = mysensors.AsyncMQTTGateway(
            pub_callback,
            sub_callback,
            in_prefix=topic_in_prefix,
            out_prefix=topic_out_prefix,
            retain=retain,
            loop=opp.loop,
            event_callback=None,
            persistence=persistence,
            persistence_file=persistence_file,
            protocol_version=version,
        )
    else:
        try:
            await opp.async_add_executor_job(is_serial_port, device)
            gateway = mysensors.AsyncSerialGateway(
                device,
                baud=baud_rate,
                loop=opp.loop,
                event_callback=None,
                persistence=persistence,
                persistence_file=persistence_file,
                protocol_version=version,
            )
        except vol.Invalid:
            try:
                await opp.async_add_executor_job(is_socket_address, device)
                # valid ip address
                gateway = mysensors.AsyncTCPGateway(
                    device,
                    port=tcp_port,
                    loop=opp.loop,
                    event_callback=None,
                    persistence=persistence,
                    persistence_file=persistence_file,
                    protocol_version=version,
                )
            except vol.Invalid:
                # invalid ip address
                _LOGGER.error("Connect failed: Invalid device %s", device)
                return None
    gateway.event_callback = event_callback
    if persistence:
        await gateway.start_persistence()

    return gateway
Exemplo n.º 5
0
async def _get_gateway(
    hass: HomeAssistantType,
    device: str,
    version: str,
    event_callback: Callable[[Message], None],
    persistence_file: Optional[str] = None,
    baud_rate: Optional[int] = None,
    tcp_port: Optional[int] = None,
    topic_in_prefix: Optional[str] = None,
    topic_out_prefix: Optional[str] = None,
    retain: bool = False,
    persistence:
    bool = True,  # old persistence option has been deprecated. kwarg is here so we can run try_connect() without persistence
) -> Optional[BaseAsyncGateway]:
    """Return gateway after setup of the gateway."""

    if persistence_file is not None:
        # interpret relative paths to be in hass config folder. absolute paths will be left as they are
        persistence_file = hass.config.path(persistence_file)

    if device == MQTT_COMPONENT:
        # what is the purpose of this?
        # if not await async_setup_component(hass, MQTT_COMPONENT, entry):
        #    return None
        mqtt = hass.components.mqtt

        def pub_callback(topic, payload, qos, retain):
            """Call MQTT publish function."""
            mqtt.async_publish(topic, payload, qos, retain)

        def sub_callback(topic, sub_cb, qos):
            """Call MQTT subscribe function."""
            @callback
            def internal_callback(msg):
                """Call callback."""
                sub_cb(msg.topic, msg.payload, msg.qos)

            hass.async_create_task(
                mqtt.async_subscribe(topic, internal_callback, qos))

        gateway = mysensors.AsyncMQTTGateway(
            pub_callback,
            sub_callback,
            in_prefix=topic_in_prefix,
            out_prefix=topic_out_prefix,
            retain=retain,
            loop=hass.loop,
            event_callback=None,
            persistence=persistence,
            persistence_file=persistence_file,
            protocol_version=version,
        )
    else:
        try:
            await hass.async_add_executor_job(is_serial_port, device)
            gateway = mysensors.AsyncSerialGateway(
                device,
                baud=baud_rate,
                loop=hass.loop,
                event_callback=None,
                persistence=persistence,
                persistence_file=persistence_file,
                protocol_version=version,
            )
        except vol.Invalid:
            try:
                await hass.async_add_executor_job(is_socket_address, device)
                # valid ip address
                gateway = mysensors.AsyncTCPGateway(
                    device,
                    port=tcp_port,
                    loop=hass.loop,
                    event_callback=None,
                    persistence=persistence,
                    persistence_file=persistence_file,
                    protocol_version=version,
                )
            except vol.Invalid:
                # invalid ip address
                _LOGGER.error("Connect failed: Invalid device %s", device)
                return None
    gateway.event_callback = event_callback
    if persistence:
        await gateway.start_persistence()

    return gateway