home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.7k stars 28.86k forks source link

Gree integration does not founds devices #114786

Open benyaming opened 2 months ago

benyaming commented 2 months ago

The problem

The integration fails to find any devices when I attempt to set it up. (I've added the devices to the Gree+ app, and they appear in my router's admin panel.)

However, after experimenting with the greeclimate package locally, I noticed that executing discovery.scan(0) initially returns 0 entries. Yet, if I run the same command again, it successfully locates all my devices. By increasing the wait_for parameter to 1, the devices are detected immediately.

The issue arises during the integration setup, where the device scan is conducted only once with a wait time of 0 seconds, resulting in no devices being found.

I'm not entirely clear on how the scanning process works. Therefore, I'm inquiring if it's possible to either extend the wait_for duration or introduce an option to manually adjust this parameter through the config flow?

Examples

With wait_for set to 0:

>>> from greeclimate.discovery import Discovery
>>> d = Discovery(8)
>>> await d.scan(0, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
[]
>>> await d.scan(0, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc7f5 @ 192.168.1.228:7000 (mac: 9424b87fc7f5)
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc63c @ 192.168.1.224:7000 (mac: 9424b87fc63c)
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fe5e3 @ 192.168.1.93:7000 (mac: 9424b87fe5e3)
[<greeclimate.device.DeviceInfo object at 0x106682910>, <greeclimate.device.DeviceInfo object at 0x106683050>, <greeclimate.device.DeviceInfo object at 0x106683410>]

With wait_for set to 1:

from greeclimate.discovery import Discovery
>>> d = Discovery(8)
>>> await d.scan(1, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc63c @ 192.168.1.224:7000 (mac: 9424b87fc63c)
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fe5e3 @ 192.168.1.93:7000 (mac: 9424b87fe5e3)
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc7f5 @ 192.168.1.228:7000 (mac: 9424b87fc7f5)
[<greeclimate.device.DeviceInfo object at 0x106683c90>, <greeclimate.device.DeviceInfo object at 0x106683510>, <greeclimate.device.DeviceInfo object at 0x106682b50>]

What version of Home Assistant Core has the issue?

2024.4.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

gree+

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 2 months ago

Hey there @cmroche, mind taking a look at this issue as it has been labeled with an integration (gree) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `gree` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign gree` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


gree documentation gree source (message by IssueLinks)

joostlek commented 2 months ago

Feel free to open a PR

Tamas-Toth-ebola commented 1 month ago

The problem

The integration fails to find any devices when I attempt to set it up. (I've added the devices to the Gree+ app, and they appear in my router's admin panel.)

However, after experimenting with the greeclimate package locally, I noticed that executing discovery.scan(0) initially returns 0 entries. Yet, if I run the same command again, it successfully locates all my devices. By increasing the wait_for parameter to 1, the devices are detected immediately.

The issue arises during the integration setup, where the device scan is conducted only once with a wait time of 0 seconds, resulting in no devices being found.

I'm not entirely clear on how the scanning process works. Therefore, I'm inquiring if it's possible to either extend the wait_for duration or introduce an option to manually adjust this parameter through the config flow?

Examples

With wait_for set to 0:

>>> from greeclimate.discovery import Discovery
>>> d = Discovery(8)
>>> await d.scan(0, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
[]
>>> await d.scan(0, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc7f5 @ 192.168.1.228:7000 (mac: 9424b87fc7f5)
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc63c @ 192.168.1.224:7000 (mac: 9424b87fc63c)
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fe5e3 @ 192.168.1.93:7000 (mac: 9424b87fe5e3)
[<greeclimate.device.DeviceInfo object at 0x106682910>, <greeclimate.device.DeviceInfo object at 0x106683050>, <greeclimate.device.DeviceInfo object at 0x106683410>]

With wait_for set to 1:

from greeclimate.discovery import Discovery
>>> d = Discovery(8)
>>> await d.scan(1, bcast_ifaces=['255.255.255.255'])
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - DEBUG - Listening for devices on 255.255.255.255
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
greeclimate.network - DEBUG - Received packet from 192.168.1.224:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc63c", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc63c @ 192.168.1.224:7000 (mac: 9424b87fc63c)
greeclimate.network - DEBUG - Received packet from 192.168.1.93:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fe5e3", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fe5e3 @ 192.168.1.93:7000 (mac: 9424b87fe5e3)
greeclimate.network - DEBUG - Received packet from 192.168.1.228:
{"t": "pack", "i": 1, "uid": 0, "cid": "", "tcid": "", "pack": {"t": "dev", "cid": "", "bc": "00000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "9424b87fc7f5", "mid": "10001", "model": "gree", "name": "", "lock": 0, "series": "gree", "vender": "1", "ver": "V3.0.0", "hid": "362001065279+U-WB05RT13V1.16.bin"}}
greeclimate.discovery - INFO - Found gree device Device: 9424b87fc7f5 @ 192.168.1.228:7000 (mac: 9424b87fc7f5)
[<greeclimate.device.DeviceInfo object at 0x106683c90>, <greeclimate.device.DeviceInfo object at 0x106683510>, <greeclimate.device.DeviceInfo object at 0x106682b50>]

What version of Home Assistant Core has the issue?

2024.4.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

gree+

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

I can confirm, that @benyaming mentioned problem, and his solution also works for me, with the important addition: For me the default wait never works! Just with the extended one...

You are our man! Please PR it, in case of you already find the call in the source...

(Interesingly the discover.py has one timeout parameter, but for me that doesn't work)

Tamas-Toth-ebola commented 1 month ago

Interestingly changing it in the 'Gree' (HA) component's __init__.py for me did not work:

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Gree Climate from a config entry."""
    hass.data.setdefault(DOMAIN, {})
    gree_discovery = DiscoveryService(hass)
    hass.data[DATA_DISCOVERY_SERVICE] = gree_discovery

    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    async def _async_scan_update(_=None):
        bcast_addr = list(await async_get_ipv4_broadcast_addresses(hass))
        #await gree_discovery.discovery.scan(0, bcast_ifaces=bcast_addr)
        await gree_discovery.discovery.scan(1, bcast_ifaces=['255.255.255.255'])

But directly in the 'greeclimate' (Python) module's discovery.py finally solved my long-term problem:

    # Discovery
    #async def scan(self, wait_for: int=0, bcast_ifaces: List[IPv4Address] | None=None) -> List[DeviceInfo]:
    async def scan(self, wait_for: int=1, bcast_ifaces: List[IPv4Address] | None=None) -> List[DeviceInfo]:
        """Sends a discovery broadcast packet on each network interface to
            locate Gree units on the network

        Args:
            wait_for (int): Optionally wait this many seconds for discovery
                            and return the devices found.

        Returns:
            List[DeviceInfo]: List of devices found during this scan
        """

You are definitely my/our man!!! Thanks!

UPDATE: Sadly just with fixed IP address in discovery.py, but at least it works now, and for me it is more than fine!

HarveyDent1 commented 1 month ago

I have the same issue. How can I install this integration manually and fix the files ?

martinblq commented 1 month ago

same issue... can we have some help please? 🙏🏻

Tamas-Toth-ebola commented 1 month ago

@HarveyDent1 @martinblq !

In case of using Docker container based HA - Core, you have to oursource your 'greeclimate' Python package to a Docker volume, as I did with the corresponding Docker compose YAML (Python version in the path depends on you HA version):

  volumes:
      - 'ha-python_package-greeclimate:/usr/local/lib/python3.12/site-packages/greeclimate/'

and

  ha-python_package-greeclimate:
    name: 'HA-Python_package-GreeClimate'
    driver: 'local'
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '{path of your corresponding outsourced folder}'

After a fresh ~ docker compose up (to apply these changes), from this point, if you change any of files of that folder, HA will use it. If you do not have files in the folder, before the config change you have to copy the current ones from the living container.

Then you can change the discovery.py, to the earlier mentioned mutation:

.
.
.
    # Discovery
    # ebola: Corrected wait time from 0 to 1
    #async def scan(self, wait_for: int=0, bcast_ifaces: List[IPv4Address] | None=None) -> List[DeviceInfo]:
    async def scan(self, wait_for: int=1, bcast_ifaces: List[IPv4Address] | None=None) -> List[DeviceInfo]:
        """Sends a discovery broadcast packet on each network interface to
            locate Gree units on the network

        Args:
            wait_for (int): Optionally wait this many seconds for discovery
                            and return the devices found.

        Returns:
            List[DeviceInfo]: List of devices found during this scan
        """
        _LOGGER.info("Scanning for Gree devices ...")

        await self.search_devices(bcast_ifaces)
        if wait_for:
            await asyncio.sleep(wait_for)
            await asyncio.gather(*self.tasks, return_exceptions=True)

        return self._device_infos
.
.
.
   async def search_on_interface(self, bcast_iface: IPv4Address) -> None:
        """Search for devices on a specific interface."""
        _LOGGER.debug(
            "Listening for devices on %s",
            bcast_iface,
        )

        if self._transport is None:
            self._transport, _ = await self._loop.create_datagram_endpoint(
                lambda: self, local_addr=("0.0.0.0", 0), allow_broadcast=True
            )

        # ebola: We have to refer directly to the device
        #await self.send({"t": "scan"}, (str(bcast_iface), 7000))
        await self.send({"t": "scan"}, ("{IP address of your climate}", 7000))
.
.
.

If you do not use HA - Core, sadly I can not help...

jmatraszek commented 3 weeks ago

Is there a way to work around this issue in HA installed in VM in Proxmox? I cannot find the greeclimate/discovery.py file anywhere...

martinblq commented 3 weeks ago

@Tamas-Toth-ebola unfortunately I use HAOS, not a docker container :/

cmroche commented 4 days ago

Enabling wait_for is not really the correct solution here. The module shouldn't be blocking HASS startup for any period of time. It's normal that discover is empty on first call, the point is to not wait and block startup while devices respond over the network.

Please enable debug logs and attach them so I can investigate why HASS is not handling those notifications when they do arrive.

jmatraszek commented 4 days ago

Hey @cmroche, I enabled debug logging in the Gree integration and here's the log:

2024-06-27 14:04:18.463 DEBUG (MainThread) [homeassistant.components.gree] Scanning network for Gree devices
2024-06-27 14:04:18.463 INFO (MainThread) [greeclimate.discovery] Scanning for Gree devices ...
2024-06-27 14:04:18.463 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 255.255.255.255
2024-06-27 14:04:18.465 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 192.168.9.255
2024-06-27 14:04:18.467 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 192.168.163.255
2024-06-27 14:04:18.468 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}
2024-06-27 14:04:18.469 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}
2024-06-27 14:04:18.469 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}
2024-06-27 14:04:21.254 DEBUG (MainThread) [homeassistant.components.gree] Scanning network for Gree devices
2024-06-27 14:04:21.254 INFO (MainThread) [greeclimate.discovery] Scanning for Gree devices ...
2024-06-27 14:04:21.254 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 255.255.255.255
2024-06-27 14:04:21.254 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 192.168.9.255
2024-06-27 14:04:21.255 DEBUG (MainThread) [greeclimate.discovery] Listening for devices on 192.168.163.255
2024-06-27 14:04:21.257 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}
2024-06-27 14:04:21.257 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}
2024-06-27 14:04:21.258 DEBUG (MainThread) [greeclimate.network] Sending packet:
{"t": "scan"}

My HAOS is added to two VLANs — the default LAN 192.168.160.0/21 and IOT vlan 192.168.9.0/24. Gree unit has IP 192.168.9.17

cmroche commented 3 days ago

@jmatraszek If you set your HASS interfaces to only use your IoT vlan, so it's only scanning one, does that address the issue?

cmroche commented 3 days ago

@jmatraszek Also, check if BCast storm protections are enabled on your switches and try turning them off.