Skip to content

Commit

Permalink
feat: allow accessing broadlink controllers by referencing static ip.…
Browse files Browse the repository at this point in the history
… This allows use of non-host network mode in docker and makes the app work properly on mac.
  • Loading branch information
jannylund committed Jul 25, 2024
1 parent 1f0928f commit 18f8c9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ version: "3.6"
services:
broadlinkmanager:
image: techblog/broadlinkmanager
network_mode: host
network_mode: host # Necessary to use discovery mode.
container_name: broadlinkmanager
restart: unless-stopped
volumes:
- ./broadlinkmanager:/opt/broadlinkmanager/data
environment:
- ENABLE_GOOGLE_ANALYTICS=True #Optional, default is True, Set to False if you want to disable Google Analytics
- STATIC_IP_LIST=192.168.1.100,192.168.1.101 # Optional. Direct ip reference to controllers in another subnet or when host networking can not be used.

```
Now open your browser and enter your docker container ip with port 7020:
Expand Down
43 changes: 31 additions & 12 deletions broadlinkmanager/broadlinkmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def get_env_ip_list():
return result


def get_static_ip_list():
static_ip_list = os.getenv("STATIC_IP_LIST", "")
result = parse_ip_list(str(static_ip_list))
logger.debug(f"Environment static IP List {result}")
return result

# Get version from version file for dynamic change


Expand Down Expand Up @@ -124,6 +130,10 @@ def GetVersionFromFle():

logger.info(f"Broadlink will try to discover devices on the following IP interfaces: {discovery_ip_address_list}")

# Assign static ips to check without discovery.
static_ip_adress_list = get_static_ip_list()
logger.info(f"Broadlink will try to access following devices directly: {static_ip_adress_list}")


# endregion

Expand Down Expand Up @@ -613,24 +623,33 @@ def search_for_devices(request: Request, freshscan: str = "1"):
return load_devices_from_file(request)
else:
logger.info("Searching for devices...")
devices = []
for interface in discovery_ip_address_list:
logger.info(f"Checking devices on interface assigned with IP: {interface}")
try:
devices = broadlink.discover(
timeout=5, local_ip_address=interface, discover_ip_address="255.255.255.255")
for device in devices:
if device.auth():
mac_address = ''.join(format(x, '02x') for x in device.mac)
logger.info(f"New device detected: {getDeviceName(device.devtype)} (ip: {device.host[0]} mac: {mac_address})")
deviceinfo = {}
deviceinfo["name"] = getDeviceName(device.devtype)
deviceinfo["type"] = format(hex(device.devtype))
deviceinfo["ip"] = device.host[0]
deviceinfo["mac"] = mac_address
result.append(deviceinfo)
devices = broadlink.discover(timeout=5, local_ip_address=interface, discover_ip_address="255.255.255.255")
except OSError as error:
logger.error(f"Error while trying to discover addresses from ip ({interface}). Error says: {error}")

for static_ip in static_ip_adress_list:
logger.info(f"Checking device with static IP: {static_ip}")
try:
device = broadlink.hello(timeout=5, host=static_ip)
devices.append(device)
except Exception as e:
logger.error(f"Error while trying to connect to static ip ({static_ip}). Error says: {e}")

for device in devices:
if device.auth():
mac_address = ''.join(format(x, '02x') for x in device.mac)
logger.info(f"New device detected: {getDeviceName(device.devtype)} (ip: {device.host[0]} mac: {mac_address})")
deviceinfo = {}
deviceinfo["name"] = getDeviceName(device.devtype)
deviceinfo["type"] = format(hex(device.devtype))
deviceinfo["ip"] = device.host[0]
deviceinfo["mac"] = mac_address
result.append(deviceinfo)

logger.debug(f"Devices Found: {str(result)}")
return JSONResponse(result)

Expand Down

0 comments on commit 18f8c9f

Please sign in to comment.