Skip to content

The agent that grows with you

simplify(compat): homeassistant_tool — drop 2 back-compat globals (_HASS_URL/_HASS_TOKEN), repoint 2 test files

Tekniumb19d424acadf
diff --git a/tests/integration/test_ha_integration.py b/tests/integration/test_ha_integration.py
index 4b619816972..9d0180c4c39 100644
--- a/tests/integration/test_ha_integration.py
+++ b/tests/integration/test_ha_integration.py
@@ -164,12 +164,8 @@ class TestToolRest:
async def test_list_entities_returns_all(self, monkeypatch):
"""_async_list_entities returns all entities from the fake server."""
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
result = await _async_list_entities()
@@ -182,12 +178,8 @@ class TestToolRest:
async def test_list_entities_domain_filter(self, monkeypatch):
"""Domain filter is applied after fetching from server."""
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
result = await _async_list_entities(domain="light")
@@ -199,12 +191,8 @@ class TestToolRest:
async def test_get_state_single_entity(self, monkeypatch):
"""_async_get_state returns full entity details."""
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
result = await _async_get_state("light.bedroom")
@@ -219,12 +207,8 @@ class TestToolRest:
import aiohttp as _aiohttp
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
with pytest.raises(_aiohttp.ClientResponseError) as exc_info:
await _async_get_state("light.nonexistent")
@@ -234,12 +218,8 @@ class TestToolRest:
async def test_call_service_turn_on(self, monkeypatch):
"""_async_call_service sends correct payload and server records it."""
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
result = await _async_call_service(
domain="light",
@@ -311,12 +291,8 @@ class TestAuthAndErrors:
import aiohttp as _aiohttp
async with FakeHAServer() as server:
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", "bad-token",
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token="bad-token": (url, token))
with pytest.raises(_aiohttp.ClientResponseError) as exc_info:
await _async_list_entities()
@@ -329,12 +305,8 @@ class TestAuthAndErrors:
async with FakeHAServer() as server:
server.force_500 = True
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_URL", server.url,
- )
- monkeypatch.setattr(
- "tools.homeassistant_tool._HASS_TOKEN", server.token,
- )
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda url=server.url, token=server.token: (url, token))
with pytest.raises(_aiohttp.ClientResponseError) as exc_info:
await _async_list_entities()
diff --git a/tests/tools/test_homeassistant_tool.py b/tests/tools/test_homeassistant_tool.py
index aab0db77d34..19af49fc2ca 100644
--- a/tests/tools/test_homeassistant_tool.py
+++ b/tests/tools/test_homeassistant_tool.py
@@ -324,8 +324,6 @@ class TestCheckAvailable:
from agent import secret_scope
from tools.homeassistant_tool import _get_config
- monkeypatch.setattr("tools.homeassistant_tool._HASS_URL", "")
- monkeypatch.setattr("tools.homeassistant_tool._HASS_TOKEN", "")
monkeypatch.setenv("HASS_URL", "http://default-profile:8123")
monkeypatch.setenv("HASS_TOKEN", "default-profile-token")
secret_scope.set_multiplex_active(True)
@@ -350,7 +348,8 @@ class TestCheckAvailable:
class TestGetHeaders:
def test_bearer_token_format(self, monkeypatch):
- monkeypatch.setattr("tools.homeassistant_tool._HASS_TOKEN", "my-secret-token")
+ monkeypatch.setattr("tools.homeassistant_tool._get_config",
+ lambda: ("http://ha.local:8123", "my-secret-token"))
headers = _get_headers()
assert headers["Authorization"] == "Bearer my-secret-token"
assert headers["Content-Type"] == "application/json"
diff --git a/tools/homeassistant_tool.py b/tools/homeassistant_tool.py
index d3fae6a9128..28bd38c0baf 100644
--- a/tools/homeassistant_tool.py
+++ b/tools/homeassistant_tool.py
@@ -16,16 +16,12 @@ from tools.registry import registry, tool_error
logger = logging.getLogger(__name__)
-# Kept for backward compatibility (e.g. test monkeypatching); prefer _get_config().
-_HASS_URL: str = ""
-_HASS_TOKEN: str = ""
-
def _get_config():
"""Return the active profile's Home Assistant URL and token."""
return (
- (_HASS_URL or get_secret("HASS_URL", "http://homeassistant.local:8123") or "").rstrip("/"),
- _HASS_TOKEN or get_secret("HASS_TOKEN", "") or "")
+ (get_secret("HASS_URL", "http://homeassistant.local:8123") or "").rstrip("/"),
+ get_secret("HASS_TOKEN", "") or "")
# Valid HA entity_id (e.g. "light.living_room", "sensor.temperature_1").