Skip to content
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
27 changes: 8 additions & 19 deletions pyfritzhome/fritzhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class Fritzhome(object):
_templates: Optional[Dict[str, FritzhomeTemplate]] = None
_triggers: Optional[Dict[str, FritzhomeTrigger]] = None

def __init__(self, host, user, password, ssl_verify=True):
def __init__(self, host, user, password, port=None, ssl_verify=True):
"""Create a fritzhome object."""
self._host = host
self._user = user
self._password = password
self._session = Session()
self._ssl_verify = ssl_verify
self._has_getdeviceinfos = True
self._has_txbusy = True
if host.startswith("https://") or host.startswith("http://"):
self.base_url = f"{host}:{port}" if port else host
else:
self.base_url = f"http://{host}:{port}" if port else f"http://{host}"
Copy link
Contributor

@flabbamann flabbamann Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should not assume http but https if the port ist 443 here, so we don't get errors if someone enters the port but not the url scheme. I'm unsure how realistic this is 😉.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this logic is just adopted from the get_prefixed_host(), so I didn't want to change the behaviour of the host field now 😬


def _request(self, url, params=None, timeout=10):
"""Send a request with parameters."""
Expand All @@ -51,7 +54,7 @@ def _request(self, url, params=None, timeout=10):

def _login_request(self, username=None, secret=None):
"""Send a login request with paramerters."""
url = self.get_prefixed_host() + "/login_sid.lua?version=2"
url = f"{self.base_url}/login_sid.lua?version=2"
params = {}
if username:
params["username"] = username
Expand All @@ -69,7 +72,7 @@ def _login_request(self, username=None, secret=None):
def _logout_request(self):
"""Send a logout request."""
_LOGGER.debug("logout")
url = self.get_prefixed_host() + "/login_sid.lua"
url = f"{self.base_url}/login_sid.lua"
params = {"security:command/logout": "1", "sid": self._sid}

self._request(url, params)
Expand Down Expand Up @@ -105,7 +108,7 @@ def _create_login_secret_md5(challenge, password):

def _aha_request(self, cmd, ain=None, param=None, rf=str):
"""Send an AHA request."""
url = self.get_prefixed_host() + "/webservices/homeautoswitch.lua"
url = f"{self.base_url}/webservices/homeautoswitch.lua"

_LOGGER.debug("self._sid:%s", self._sid)

Expand Down Expand Up @@ -152,20 +155,6 @@ def logout(self):
self._logout_request()
self._sid = None

def get_prefixed_host(self):
"""Choose the correct protocol prefix for the host.

Supports three input formats:
- https://<host>(requests use strict certificate validation by default)
- http://<host> (unecrypted)
- <host> (unencrypted)
"""
host = self._host
if host.startswith("https://") or host.startswith("http://"):
return host
else:
return "http://" + host

def update_devices(self, ignore_removed=True):
"""Update the device."""
_LOGGER.info("Updating Devices ...")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_fritzhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def test_not_logged_in_error(self):
self.fritz.update_devices()
assert str(ex.value) == "not logged in, login before doing any requests."

@pytest.mark.parametrize(
("host", "port", "expected_base_url"),
[
("10.0.0.1", None, "http://10.0.0.1"),
("http://10.0.0.1", None, "http://10.0.0.1"),
("https://10.0.0.1", None, "https://10.0.0.1"),
("10.0.0.1", 1234, "http://10.0.0.1:1234"),
("http://10.0.0.1", 1234, "http://10.0.0.1:1234"),
("https://10.0.0.1", 1234, "https://10.0.0.1:1234"),
],
)
def test_base_url(self, host: str, port: int, expected_base_url: str):
fritz = Fritzhome(host, "user", "admin123", port)
assert fritz.base_url == expected_base_url

def test_aha_request(self):
self.fritz._aha_request(cmd="testcmd")
self.fritz._request.assert_called_with(
Expand Down