fairecasoimeme / ZiGate-Ethernet

Ethernet platform for PiZiGate
9 stars 3 forks source link

[REQUEST] Zeroconf DNS TXT parameters in ZiGate Ethernet to support Home Assistant ZHA network discovery #7

Open Hedda opened 3 years ago

Hedda commented 3 years ago

Please consider adding Zeroconf DNS TXT Reconds parameters in ZiGate Ethernet firmware for auto network discovery by ZHA.

Home Assistant OS (formerly HASSIO) support automatic local network scanning and “Service Discovery” via local mDNS Zeroconf (Multicast DNS Zero-configuration networking) and when the DNS name of a local Zigbee serial gateway is discovered that has already been whitelisted it can pass along that to a domain like the ZHA integration component in Home Assistant.

https://www.home-assistant.io/integrations/zha/#discovery-via-usb-or-zeroconf

For more context about this Zigbee gateway “Service Discovery” via Zeroconf for the ZHA integration in Home Assistant, pease see:

https://community.home-assistant.io/t/zha-automatic-discovery-of-zigbee-coordinator-bridges-gateways-ethernet-wifi-network-devices-that-support-zeroconf-or-ssdp/293300

and

https://developers.home-assistant.io/docs/creating_integration_manifest/#zeroconf

FYI, there is already a working proof-of-concept this is now supported by Tube's Zigbee gateways (based on ESPHome firmware):

https://www.home-assistant.io/integrations/zha#discovery-via-usb-or-zeroconf

https://github.com/tube0013/tube_gateways

Once support for Zeroconf has been added to the firmware you also need the firmware to provide DNS TXT records for passing along setting parameters to integration config flows.

DNS TXT records is used to pass along recommended settings parameters config flow to Home Assistant's ZHA integration:

version=1.0
radio_type=zigate
baud_rate=value
data_flow_control=software
tcp_port_serial_gateway=value

I believe that is advertised for Zeroconf config in this format or similar:

zha_ezsp_zeroconf  _ezsp._tcp  local
   hostname = [zha_ezsp_zeroconf.local]
   port = [8080]
   protocol = [tcp]
   service = tubes_zb_gw
   txt = ["version=1.0"]
   txt = ["radio_type=ezsp"]
   txt = ["baud_rate=value"]
   txt = ["data_flow_control=software"]

(use e.g. avahi-browse -r -a to see this)

As can see, you will need one DNS TXT Record for each attribute and value that is to be passed along to HA's ZHA integration.

Zeroconf DNS TXT records could also be used to pass along info about hostname, versions, location, MAC address, and more, etc..

http://www.zeroconf.org/Rendezvous/txtrecords.html

https://en.wikipedia.org/wiki/Zero-configuration_networking#DNS-based_service_discovery

https://en.wikipedia.org/wiki/TXT_record

More information about idea of using DNS TXT records for passing along setting parameters in https://github.com/thegroove/esphome-zbbridge#1 and https://github.com/thegroove/esphome-zha-ezsp-zeroconf

fairecasoimeme commented 3 years ago

Hi, I add zeroconf service to master tag.


  MDNS.addService("zigbee_gateway", "tcp", 9999);
  MDNS.addServiceTxt("zigbee_gateway", "tcp", "radio_type", "zigate");
  MDNS.addServiceTxt("zigbee_gateway", "tcp", "baud_rate", "115200");
  MDNS.addServiceTxt("zigbee_gateway", "tcp", "tcp_port_serial_gateway", "9999");

image

Hedda commented 3 years ago

Nice, but noticed now that Zeroconf does not provide "service" (zigate-gateway), "protocol" (tcp), and port (9999) via TXT Records.

It does hower need TXT Record for "data_flow_control" value "software" or "hardware" = hardware flow control disabled or enabled.

Better probably to see and compare with actual example config for ESPHome as in Tube's Zigbee Gateway as that is the reference:

https://github.com/tube0013/tube_gateways/blob/main/V2_tube_zb_gw_cc2752p2/ESPHome/tube_zb_gw_cc2652p2v2.yml

zeroconf:
  - service: tubes_zb_gw
    protocol: tcp
    port: 6638
    txt:
      version: 1.0
      radio_type: znp
      baud_rate: 115200
      data_flow_control: software

Support for Tube's Zigbee Gateway was initially added to Home Aassistant core for ZHA support in https://github.com/home-assistant/core/pull/48420

As I believe to then whitelist Zeroconf for more radio types like "zigate" in HA's zeroconf as well as for ZHA need to do PR for:

https://github.com/home-assistant/core/blob/dev/homeassistant/generated/zeroconf.py

 "_esphomelib._tcp.local.": [
        {
            "domain": "zha",
            "name": "tube*"
        }
    ],

and

https://github.com/home-assistant/core/blob/dev/homeassistant/components/zha/manifest.json

"zeroconf": [
    {
      "type": "_esphomelib._tcp.local.",
      "name": "tube*"
    }
  ],

and

https://github.com/home-assistant/core/blob/dev/homeassistant/components/zha/config_flow.py

async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType):
        """Handle zeroconf discovery."""
        # Hostname is format: livingroom.local.
        local_name = discovery_info["hostname"][:-1]
        node_name = local_name[: -len(".local")]
        host = discovery_info[CONF_HOST]
        device_path = f"socket://{host}:6638"

        if current_entry := await self.async_set_unique_id(node_name):
            self._abort_if_unique_id_configured(
                updates={
                    CONF_DEVICE: {
                        **current_entry.data.get(CONF_DEVICE, {}),
                        CONF_DEVICE_PATH: device_path,
                    },
                }
            )

        # Check if already configured
        if self._async_current_entries():
            return self.async_abort(reason="single_instance_allowed")

        self.context["title_placeholders"] = {
            CONF_NAME: node_name,
        }

        self._device_path = device_path
        self._radio_type = (
            RadioType.ezsp.name if "efr32" in local_name else RadioType.znp.name
        )

By the way, If users local network supports .local mDNS addresses does it now get DNS name "zigate-ethernet.local" as standard or?

PS: I don't own any Zigate products myself you maybe @doudz can test as the developer of https://github.com/zigpy/zigpy-zigate ?

pipiche38 commented 3 years ago

@Hedda it might be better to provide a PR , that would ease everybody

Hedda commented 3 years ago

Sorry but don't think I have the coding skills to add this to ZiGate-Ethernet firmware, nor do I own a ZiGate-Ethernet to test it.

FYI, there's however a ZiGate-Ethernet fork here for ZigStar-Gateway with mDNS + TXT records feature for Zeroconf with ZHA:

https://github.com/xyzroe/ZigStarGW-FW

https://github.com/xyzroe/ZigStarGW-FW/issues/3

https://github.com/xyzroe/ZigStarGW-FW/blob/fd41317584758333786d26592d24b3d90a0e60d2/src/main.cpp#L648

 MDNS.addService("zig_star_gw", "tcp", ConfigSettings.socketPort);
    MDNS.addServiceTxt("zig_star_gw", "tcp", "version", "1.0");
    MDNS.addServiceTxt("zig_star_gw", "tcp", "radio_type", "znp");
    MDNS.addServiceTxt("zig_star_gw", "tcp", "baud_rate", String(ConfigSettings.serialSpeed));
    MDNS.addServiceTxt("zig_star_gw", "tcp", "data_flow_control", "software");

To me it looks like compared to this upstream ZiGate-Ethernet they added data_flow_control and hardcoded it to software.

I am not even sure if ZiGate Zigbee radio uses or supports software, hardware, or no flow control for serial communication?

Also looks like they have moved both socketPort (port) + baud_rate configuration values to the web interface settings but other values are still hardcoded(?):

https://github.com/xyzroe/ZigStarGW-FW/blob/e1530ec538435aca966619572f903afedd131ea4/src/web.cpp#L674

Hopefully, someone with more coding skills than I can backport this feature from that fork once been tested + confirmed working?

You should be able to test ZiGate results obtained with avahi-browse -a -r so that can see that txt format and info looks correct.

A result from avahi-browse -a -r that should be compatible with ZHA integration in Home Assistant should look something like:

_zigate_zigbee_gateway._tcp      local
   hostname = [zigate_zigbee_gateway.local]
   address = [192.168.0.174]
   port = [9999]
   txt = ["radio_type=zigate" "baud_rate=115200" "data_flow_control=software"]

Note that the service name really needs to start with the word "zigate" as an non-general unique identifier for ZiGate-Ethernet.

Once all those values are available to Zeroconf via DNS TXT records can submit patch for ZiGate-Ethernet to Home Assistant core.

Hedda commented 3 years ago

Can someone who owns a ZiGate-Ethernet please test this PR? -> https://github.com/fairecasoimeme/ZiGate-Ethernet/pull/8

Hedda commented 2 years ago

FYI, Zero-configuration networking (zeroconf) looks to have been changed and improved in the Home Assistant 2022.02 release:

https://github.com/home-assistant/core/pull/62133

https://www.home-assistant.io/integrations/zeroconf/

This change is only a concern for custom integration developers.

Currently zeroconf matching only allows matching the macaddress, model, and manufacturer properties along with the name from the ZeroconfServiceInfo.

Since properties are arbitrarily defined by the zeroconf service, the list of named properties has grown over time.

Matching now allows for any arbitrarily defined property. All property matches must be lowercase, wildcards are supported

The top level keys model, manufacturer, and macaddress are now deprecated from components manifest.json file and should be moved into a properties dict.

For example:

-    {"type":"_airplay._tcp.local.","model":"appletv*"}
+    {"type":"_airplay._tcp.local.","properties":{"model":"appletv*"}}

By the way, Home Assistant founders/leads now explicitly said this year they will prioritize focus on features like these types:

https://www.home-assistant.io/blog/2022/01/19/streamlining-experiences/

Check out this video at around 11 min 55 sec in where talk about that focus point about making it easier to get started, etc.

https://www.youtube.com/watch?v=t_2D_KoFIfU&t=710s

Hopefully, it should be a greater chance of getting more help from other Home Assistant developers with this if ask them.

Hedda commented 2 years ago

Any ZHA users/devs with ZiGate Ethernet hardware out there willing to take another stab at adding Zeroconf support for Zigate?

See:

https://www.home-assistant.io/integrations/zha#discovery-via-usb-or-zeroconf

and

https://community.home-assistant.io/t/zha-automatic-network-discovery-of-zigbee-coordinator-bridges-gateways-ethernet-wifi-network-adapters-that-support-zeroconf-or-ssdp/293300

I do not have ZiGate hardware myself but did a ZHA pull request blind for Home Assistant core here that could be used as a reference:

https://github.com/home-assistant/core/pull/58467

It is a relativly small change but it needs to be tested by someone with ZiGate Ethernet hardware before it will be merged into HA.

Anyway, the missing DNS records for Zeroconf discovery has now been merged to ZiGate Ethernet firmware by @fairecasoimeme here:

https://github.com/fairecasoimeme/ZiGate-Ethernet/pull/8

Hedda commented 2 years ago

FYI, for reference, noticed that fairecasoimeme is working on a PR for Home Assistant which will add ZiGate discovery support:

https://github.com/home-assistant/core/pull/68577

"Add ZiGate device on automatic integration USB and ZEROCONF"

https://www.home-assistant.io/integrations/zha#discovery-via-usb-or-zeroconf

That will add automatic ZeroConf (MDNS) discovery of ZiGate Ethernet Gateway (not WiFi Gateway) + ZiGate USB adapters.