def load_mgr_from_file(mgr: openzwavemqtt.OZWManager, file_path):
    with open(file_path, "rt") as fp:
        for line in fp:
            topic, payload = line.strip().split(",", 1)
            try:
                mgr.receive_message(topic, payload)
            except ValueError:
                raise ExitException(
                    f"Unable to process message on topic {topic} as JSON: {payload}"
                )
def load_mgr_from_file(mgr: openzwavemqtt.OZWManager, file_path: str) -> None:
    """Load manager from file."""
    with open(file_path, "rt", encoding="utf-8") as fp:
        for line in fp:
            topic, payload = line.strip().split(",", 1)
            try:
                mgr.receive_message(topic, payload)
            except ValueError as err:
                raise ExitException(
                    f"Unable to process message on topic {topic} as JSON: {payload}"
                ) from err
async def run_client() -> None:
    """Run client."""
    client = MQTTClient("localhost")
    options = OZWOptions(
        send_message=client.send_message, topic_prefix=f"{TOPIC_OPENZWAVE}/"
    )
    manager = OZWManager(options)

    await client.start_client(manager)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up ozw from a config entry."""
    ozw_data = hass.data[DOMAIN][entry.entry_id] = {}
    ozw_data[DATA_UNSUBSCRIBE] = []

    data_nodes = {}
    data_values = {}
    removed_nodes = []

    @callback
    def send_message(topic, payload):
        mqtt.async_publish(hass, topic, json.dumps(payload))

    options = OZWOptions(send_message=send_message,
                         topic_prefix=f"{TOPIC_OPENZWAVE}/")
    manager = OZWManager(options)

    hass.data[DOMAIN][MANAGER] = manager
    hass.data[DOMAIN][OPTIONS] = options

    @callback
    def async_node_added(node):
        # Caution: This is also called on (re)start.
        _LOGGER.debug("[NODE ADDED] node_id: %s", node.id)
        data_nodes[node.id] = node
        if node.id not in data_values:
            data_values[node.id] = []

    @callback
    def async_node_changed(node):
        _LOGGER.debug("[NODE CHANGED] node_id: %s", node.id)
        data_nodes[node.id] = node
        # notify devices about the node change
        if node.id not in removed_nodes:
            hass.async_create_task(async_handle_node_update(hass, node))

    @callback
    def async_node_removed(node):
        _LOGGER.debug("[NODE REMOVED] node_id: %s", node.id)
        data_nodes.pop(node.id)
        # node added/removed events also happen on (re)starts of hass/mqtt/ozw
        # cleanup device/entity registry if we know this node is permanently deleted
        # entities itself are removed by the values logic
        if node.id in removed_nodes:
            hass.async_create_task(async_handle_remove_node(hass, node))
            removed_nodes.remove(node.id)

    @callback
    def async_instance_event(message):
        event = message["event"]
        event_data = message["data"]
        _LOGGER.debug("[INSTANCE EVENT]: %s - data: %s", event, event_data)
        # The actual removal action of a Z-Wave node is reported as instance event
        # Only when this event is detected we cleanup the device and entities from hass
        # Note: Find a more elegant way of doing this, e.g. a notification of this event from OZW
        if event in ["removenode", "removefailednode"
                     ] and "Node" in event_data:
            removed_nodes.append(event_data["Node"])

    @callback
    def async_value_added(value):
        node = value.node
        # Clean up node.node_id and node.id use. They are the same.
        node_id = value.node.node_id

        # Filter out CommandClasses we're definitely not interested in.
        if value.command_class in [
                CommandClass.MANUFACTURER_SPECIFIC,
        ]:
            return

        _LOGGER.debug(
            "[VALUE ADDED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )

        node_data_values = data_values[node_id]

        # Check if this value should be tracked by an existing entity
        value_unique_id = create_value_id(value)
        for values in node_data_values:
            values.async_check_value(value)
            if values.values_id == value_unique_id:
                return  # this value already has an entity

        # Run discovery on it and see if any entities need created
        for schema in DISCOVERY_SCHEMAS:
            if not check_node_schema(node, schema):
                continue
            if not check_value_schema(
                    value, schema[const.DISC_VALUES][const.DISC_PRIMARY]):
                continue

            values = ZWaveDeviceEntityValues(hass, options, schema, value)
            values.async_setup()

            # This is legacy and can be cleaned up since we are in the main thread:
            # We create a new list and update the reference here so that
            # the list can be safely iterated over in the main thread
            data_values[node_id] = node_data_values + [values]

    @callback
    def async_value_changed(value):
        # if an entity belonging to this value needs updating,
        # it's handled within the entity logic
        _LOGGER.debug(
            "[VALUE CHANGED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )
        # Handle a scene activation message
        if value.command_class in [
                CommandClass.SCENE_ACTIVATION,
                CommandClass.CENTRAL_SCENE,
        ]:
            async_handle_scene_activated(hass, value)
            return

    @callback
    def async_value_removed(value):
        _LOGGER.debug(
            "[VALUE REMOVED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )
        # signal all entities using this value for removal
        value_unique_id = create_value_id(value)
        async_dispatcher_send(hass, const.SIGNAL_DELETE_ENTITY,
                              value_unique_id)
        # remove value from our local list
        node_data_values = data_values[value.node.id]
        node_data_values[:] = [
            item for item in node_data_values
            if item.values_id != value_unique_id
        ]

    # Listen to events for node and value changes
    for event, event_callback in (
        (EVENT_NODE_ADDED, async_node_added),
        (EVENT_NODE_CHANGED, async_node_changed),
        (EVENT_NODE_REMOVED, async_node_removed),
        (EVENT_VALUE_ADDED, async_value_added),
        (EVENT_VALUE_CHANGED, async_value_changed),
        (EVENT_VALUE_REMOVED, async_value_removed),
        (EVENT_INSTANCE_EVENT, async_instance_event),
    ):
        ozw_data[DATA_UNSUBSCRIBE].append(options.listen(
            event, event_callback))

    # Register Services
    services = ZWaveServices(hass, manager)
    services.async_register()

    # Register WebSocket API
    async_register_api(hass)

    @callback
    def async_receive_message(msg):
        manager.receive_message(msg.topic, msg.payload)

    async def start_platforms():
        await asyncio.gather(*[
            hass.config_entries.async_forward_entry_setup(entry, component)
            for component in PLATFORMS
        ])
        ozw_data[DATA_UNSUBSCRIBE].append(await mqtt.async_subscribe(
            hass, f"{TOPIC_OPENZWAVE}/#", async_receive_message))

    hass.async_create_task(start_platforms())

    return True
Exemple #5
0
async def async_setup_entry(  # noqa: C901
        hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up ozw from a config entry."""
    hass.data.setdefault(DOMAIN, {})
    ozw_data = hass.data[DOMAIN][entry.entry_id] = {}
    ozw_data[DATA_UNSUBSCRIBE] = []

    data_nodes = {}
    hass.data[DOMAIN][NODES_VALUES] = data_values = {}
    removed_nodes = []
    manager_options = {"topic_prefix": f"{TOPIC_OPENZWAVE}/"}

    if entry.unique_id is None:
        hass.config_entries.async_update_entry(entry, unique_id=DOMAIN)

    if entry.data.get(CONF_USE_ADDON):
        # Do not use MQTT integration. Use own MQTT client.
        # Retrieve discovery info from the OpenZWave add-on.
        discovery_info = await hass.components.hassio.async_get_addon_discovery_info(
            "core_zwave")

        if not discovery_info:
            _LOGGER.error("Failed to get add-on discovery info")
            raise ConfigEntryNotReady

        discovery_info_config = discovery_info["config"]

        host = discovery_info_config["host"]
        port = discovery_info_config["port"]
        username = discovery_info_config["username"]
        password = discovery_info_config["password"]
        mqtt_client = MQTTClient(host,
                                 port,
                                 username=username,
                                 password=password)
        manager_options["send_message"] = mqtt_client.send_message

    else:
        mqtt_entries = hass.config_entries.async_entries("mqtt")
        if not mqtt_entries or mqtt_entries[
                0].state is not ConfigEntryState.LOADED:
            _LOGGER.error("MQTT integration is not set up")
            return False

        mqtt_entry = mqtt_entries[0]  # MQTT integration only has one entry.

        @callback
        def send_message(topic, payload):
            if mqtt_entry.state is not ConfigEntryState.LOADED:
                _LOGGER.error("MQTT integration is not set up")
                return

            mqtt.async_publish(hass, topic, json.dumps(payload))

        manager_options["send_message"] = send_message

    options = OZWOptions(**manager_options)
    manager = OZWManager(options)

    hass.data[DOMAIN][MANAGER] = manager

    @callback
    def async_node_added(node):
        # Caution: This is also called on (re)start.
        _LOGGER.debug("[NODE ADDED] node_id: %s", node.id)
        data_nodes[node.id] = node
        if node.id not in data_values:
            data_values[node.id] = []

    @callback
    def async_node_changed(node):
        _LOGGER.debug("[NODE CHANGED] node_id: %s", node.id)
        data_nodes[node.id] = node
        # notify devices about the node change
        if node.id not in removed_nodes:
            hass.async_create_task(async_handle_node_update(hass, node))

    @callback
    def async_node_removed(node):
        _LOGGER.debug("[NODE REMOVED] node_id: %s", node.id)
        data_nodes.pop(node.id)
        # node added/removed events also happen on (re)starts of hass/mqtt/ozw
        # cleanup device/entity registry if we know this node is permanently deleted
        # entities itself are removed by the values logic
        if node.id in removed_nodes:
            hass.async_create_task(async_handle_remove_node(hass, node))
            removed_nodes.remove(node.id)

    @callback
    def async_instance_event(message):
        event = message["event"]
        event_data = message["data"]
        _LOGGER.debug("[INSTANCE EVENT]: %s - data: %s", event, event_data)
        # The actual removal action of a Z-Wave node is reported as instance event
        # Only when this event is detected we cleanup the device and entities from hass
        # Note: Find a more elegant way of doing this, e.g. a notification of this event from OZW
        if event in ("removenode",
                     "removefailednode") and "Node" in event_data:
            removed_nodes.append(event_data["Node"])

    @callback
    def async_value_added(value):
        node = value.node
        # Clean up node.node_id and node.id use. They are the same.
        node_id = value.node.node_id

        # Filter out CommandClasses we're definitely not interested in.
        if value.command_class in (CommandClass.MANUFACTURER_SPECIFIC, ):
            return

        _LOGGER.debug(
            "[VALUE ADDED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )

        node_data_values = data_values[node_id]

        # Check if this value should be tracked by an existing entity
        value_unique_id = create_value_id(value)
        for values in node_data_values:
            values.async_check_value(value)
            if values.values_id == value_unique_id:
                return  # this value already has an entity

        # Run discovery on it and see if any entities need created
        for schema in DISCOVERY_SCHEMAS:
            if not check_node_schema(node, schema):
                continue
            if not check_value_schema(
                    value, schema[const.DISC_VALUES][const.DISC_PRIMARY]):
                continue

            values = ZWaveDeviceEntityValues(hass, options, schema, value)
            values.async_setup()

            # This is legacy and can be cleaned up since we are in the main thread:
            # We create a new list and update the reference here so that
            # the list can be safely iterated over in the main thread
            data_values[node_id] = node_data_values + [values]

    @callback
    def async_value_changed(value):
        # if an entity belonging to this value needs updating,
        # it's handled within the entity logic
        _LOGGER.debug(
            "[VALUE CHANGED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )
        # Handle a scene activation message
        if value.command_class in (
                CommandClass.SCENE_ACTIVATION,
                CommandClass.CENTRAL_SCENE,
        ):
            async_handle_scene_activated(hass, value)
            return

    @callback
    def async_value_removed(value):
        _LOGGER.debug(
            "[VALUE REMOVED] node_id: %s - label: %s - value: %s - value_id: %s - CC: %s",
            value.node.id,
            value.label,
            value.value,
            value.value_id_key,
            value.command_class,
        )
        # signal all entities using this value for removal
        value_unique_id = create_value_id(value)
        async_dispatcher_send(hass, const.SIGNAL_DELETE_ENTITY,
                              value_unique_id)
        # remove value from our local list
        node_data_values = data_values[value.node.id]
        node_data_values[:] = [
            item for item in node_data_values
            if item.values_id != value_unique_id
        ]

    # Listen to events for node and value changes
    for event, event_callback in (
        (EVENT_NODE_ADDED, async_node_added),
        (EVENT_NODE_CHANGED, async_node_changed),
        (EVENT_NODE_REMOVED, async_node_removed),
        (EVENT_VALUE_ADDED, async_value_added),
        (EVENT_VALUE_CHANGED, async_value_changed),
        (EVENT_VALUE_REMOVED, async_value_removed),
        (EVENT_INSTANCE_EVENT, async_instance_event),
    ):
        ozw_data[DATA_UNSUBSCRIBE].append(options.listen(
            event, event_callback))

    # Register Services
    services = ZWaveServices(hass, manager)
    services.async_register()

    # Register WebSocket API
    async_register_api(hass)

    @callback
    def async_receive_message(msg):
        manager.receive_message(msg.topic, msg.payload)

    async def start_platforms():
        await asyncio.gather(
            *(hass.config_entries.async_forward_entry_setup(entry, platform)
              for platform in PLATFORMS))
        if entry.data.get(CONF_USE_ADDON):
            mqtt_client_task = asyncio.create_task(
                mqtt_client.start_client(manager))

            async def async_stop_mqtt_client(event=None):
                """Stop the mqtt client.

                Do not unsubscribe the manager topic.
                """
                mqtt_client_task.cancel()
                with suppress(asyncio.CancelledError):
                    await mqtt_client_task

            ozw_data[DATA_UNSUBSCRIBE].append(
                hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
                                           async_stop_mqtt_client))
            ozw_data[DATA_STOP_MQTT_CLIENT] = async_stop_mqtt_client

        else:
            ozw_data[DATA_UNSUBSCRIBE].append(await mqtt.async_subscribe(
                hass, f"{manager.options.topic_prefix}#",
                async_receive_message))

    hass.async_create_task(start_platforms())

    return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up zwave_mqtt from a config entry."""

    @callback
    def async_receive_message(msg):
        manager.receive_message(msg.topic, msg.payload)

    platforms_loaded = []

    async def mark_platform_loaded(platform):
        platforms_loaded.append(platform)

        if len(platforms_loaded) != len(PLATFORMS):
            return

        hass.data[DOMAIN][entry.entry_id]["unsubscribe"] = await mqtt.async_subscribe(
            hass, f"{TOPIC_OPENZWAVE}/#", async_receive_message
        )

    hass.data[DOMAIN][entry.entry_id] = {"mark_platform_loaded": mark_platform_loaded}

    data_nodes = {}
    data_values = {}

    @callback
    def send_message(topic, payload):
        _LOGGER.debug("sending message to topic %s", topic)
        mqtt.async_publish(hass, topic, json.dumps(payload))

    options = OZWOptions(send_message=send_message, topic_prefix=f"{TOPIC_OPENZWAVE}/")
    manager = OZWManager(options)

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component)
        )

    @callback
    def async_node_added(node):
        _LOGGER.debug("NODE ADDED: %s - node id %s", node, node.id)
        data_nodes[node.id] = node
        data_values[node.id] = []

    @callback
    def async_node_changed(node):
        _LOGGER.debug("NODE CHANGED: %s", node)
        data_nodes[node.id] = node

    @callback
    def async_value_added(value):
        node = value.node
        node_id = value.node.node_id

        # temporary if statement to cut down on number of debug log lines
        if value.command_class not in [
            "COMMAND_CLASS_CONFIGURATION",
            "COMMAND_CLASS_VERSION",
        ]:
            _LOGGER.debug(
                "VALUE ADDED: node %s - value label %s - value %s -- id %s -- cc %s",
                value.node.id,
                value.label,
                value.value,
                value.value_id_key,
                value.command_class,
            )

        node_data_values = data_values[node_id]

        # Check if this value should be tracked by an existing entity
        for values in node_data_values:
            values.check_value(value)

        # Run discovery on it and see if any entities need created
        for schema in DISCOVERY_SCHEMAS:
            if not check_node_schema(node, schema):
                continue
            if not check_value_schema(
                value, schema[const.DISC_VALUES][const.DISC_PRIMARY]
            ):
                continue

            values = ZWaveDeviceEntityValues(hass, options, schema, value)

            # We create a new list and update the reference here so that
            # the list can be safely iterated over in the main thread
            data_values[node_id] = node_data_values + [values]

    @callback
    def async_value_changed(value):
        _LOGGER.debug(
            "VALUE CHANGED: node %s - value label %s - value %s",
            value.node,
            value.label,
            value.value,
        )
        # Handle a scene activation message
        if value.command_class in [
            "COMMAND_CLASS_SCENE_ACTIVATION",
            "COMMAND_CLASS_CENTRAL_SCENE",
        ]:
            handle_scene_activated(hass, value)
            return

    # Listen to events for node and value changes
    options.listen(EVENT_NODE_ADDED, async_node_added)
    options.listen(EVENT_VALUE_ADDED, async_value_added)
    options.listen(EVENT_NODE_CHANGED, async_node_changed)
    options.listen(EVENT_VALUE_CHANGED, async_value_changed)

    # Register Services
    services = ZWaveServices(hass, manager, data_nodes)
    services.register()

    return True