Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow accessing broadlink controllers by referencing static ip.… #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.extend(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(static_ip, timeout=5)
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