Skip to content

Commit

Permalink
Scan additional ports
Browse files Browse the repository at this point in the history
  • Loading branch information
nbogojevic committed Jan 26, 2024
1 parent b39d77e commit 5b94cb5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
63 changes: 32 additions & 31 deletions midea_beautiful/lan.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from midea_beautiful.midea import (
APPLIANCE_TYPE_DEHUMIDIFIER,
DEFAULT_RETRIES,
DISCOVERY_PORT,
DISCOVERY_PORTS,
MSGTYPE_ENCRYPTED_REQUEST,
MSGTYPE_HANDSHAKE_REQUEST,
)
Expand Down Expand Up @@ -995,39 +995,40 @@ def appliance_state(
key = key or ""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(timeout)
port = DISCOVERY_PORT
for port in DISCOVERY_PORTS:

try:
# Connect to the appliance
sock.connect((address, port))
try:
# Connect to the appliance
sock.connect((address, port))

# Send the discovery query
_LOGGER.debug(
"Sending to %s:%d %s", Redacted(address, 5), port, DISCOVERY_MSG
)
sock.sendall(DISCOVERY_MSG)
# Send the discovery query
_LOGGER.debug(
"Sending to %s:%d %s", Redacted(address, 5), port, DISCOVERY_MSG
)
sock.sendall(DISCOVERY_MSG)

# Received data
response = sock.recv(512)
_LOGGER.debug(
"Received from %s:%d %s", Redacted(address, 5), port, response
)
appliance = LanDevice(
data=response, token=token, key=key, security=security
)
appliance.max_retries = retries
appliance.socket_timeout = timeout
_LOGGER.debug("Appliance %s", appliance)
except socket.timeout as ex:
raise MideaNetworkError(
f"Timeout while connecting to appliance {address}:{port}"
) from ex
except socket.error as ex:
raise MideaNetworkError(
f"Could not connect to appliance {address}:{port}"
) from ex
finally:
sock.close()
# Received data
response = sock.recv(512)
_LOGGER.debug(
"Received from %s:%d %s", Redacted(address, 5), port, response
)
appliance = LanDevice(
data=response, token=token, key=key, security=security
)
appliance.max_retries = retries
appliance.socket_timeout = timeout
_LOGGER.debug("Appliance %s", appliance)
break
except socket.timeout as ex:
raise MideaNetworkError(
f"Timeout while connecting to appliance {address}:{port}"
) from ex
except socket.error as ex:
raise MideaNetworkError(
f"Could not connect to appliance {address}:{port}"
) from ex
finally:
sock.close()
elif appliance_id is not None:
if use_cloud and cloud:
appliance = LanDevice(
Expand Down
3 changes: 1 addition & 2 deletions midea_beautiful/midea.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
MSGTYPE_ENCRYPTED_REQUEST: Final = 0x6
MSGTYPE_TRANSPARENT: Final = 0xF

DISCOVERY_PORT: Final = 6445

DISCOVERY_PORTS: Final = [6445, 20086]

INTERNAL_KEY = unhexlify(
"c8aa6c57402cac8b5674db84acc89be82c704362da72b5ff21544b483b50d39c"
Expand Down
13 changes: 7 additions & 6 deletions midea_beautiful/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from midea_beautiful.appliance import Appliance
from midea_beautiful.cloud import MideaCloud
from midea_beautiful.lan import DISCOVERY_MSG, LanDevice, matches_lan_cloud
from midea_beautiful.midea import DEFAULT_RETRIES, DEFAULT_TIMEOUT, DISCOVERY_PORT
from midea_beautiful.midea import DEFAULT_RETRIES, DEFAULT_TIMEOUT, DISCOVERY_PORTS
from midea_beautiful.util import Redacted, is_very_verbose

# pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -72,11 +72,12 @@ def _broadcast_message(self, addresses: list[str]) -> None:
for addr in addresses:
_LOGGER.debug("Broadcasting to %s", addr)
try:
if is_very_verbose():
_LOGGER.debug(
"UDP broadcast %s:%d %s", addr, DISCOVERY_PORT, DISCOVERY_MSG
)
self._socket.sendto(DISCOVERY_MSG, (addr, DISCOVERY_PORT))
for port in DISCOVERY_PORTS:
if is_very_verbose():
_LOGGER.debug(
"UDP broadcast %s:%d %s", addr, port, DISCOVERY_MSG
)
self._socket.sendto(DISCOVERY_MSG, (addr, port))
except Exception as ex: # pylint: disable=broad-except
_LOGGER.debug("Unable to send broadcast to: %s cause %s", addr, ex)

Expand Down

0 comments on commit 5b94cb5

Please sign in to comment.