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

add home by bridge api to my tado #134

Merged
merged 5 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 12 additions & 11 deletions PyTado/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Domain(enum.StrEnum):
HOME = "homes"
DEVICES = "devices"
ME = "me"
HOME_BY_BRIDGE = "homeByBridge"


class Action(enum.StrEnum):
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(
action: Action | str = Action.GET,
payload: dict[str, Any] | None = None,
domain: Domain = Domain.HOME,
device: int | None = None,
device: int | str | None = None,
mode: Mode = Mode.OBJECT,
params: dict[str, Any] | None = None,
) -> None:
Expand All @@ -89,7 +90,7 @@ def __init__(
action: Action | str = Action.GET,
payload: dict[str, Any] | None = None,
domain: Domain = Domain.HOME,
device: int | None = None,
device: int | str | None = None,
wmalgadey marked this conversation as resolved.
Show resolved Hide resolved
mode: Mode = Mode.OBJECT,
params: dict[str, Any] | None = None,
) -> None:
Expand Down Expand Up @@ -181,6 +182,7 @@ def request(self, request: TadoRequest) -> dict[str, Any]:

http_request = requests.Request(method=request.action, url=url, headers=headers, data=data)
prepped = http_request.prepare()
prepped.hooks["response"].append(self._log_response)

retries = _DEFAULT_RETRIES

Expand All @@ -196,14 +198,15 @@ def request(self, request: TadoRequest) -> dict[str, Any]:
_LOGGER.warning("Connection error: %s", e)
self._session.close()
self._session = requests.Session()
self._session.hooks["response"].append(self._log_response)
retries -= 1
else:
_LOGGER.error(
"Connection failed after %d retries: %s",
_DEFAULT_RETRIES,
e,
)
raise TadoException(e)
raise TadoException(e) from e

if response.text is None or response.text == "":
return {}
Expand All @@ -213,19 +216,17 @@ def request(self, request: TadoRequest) -> dict[str, Any]:
def _configure_url(self, request: TadoRequest) -> str:
if request.endpoint == Endpoint.MOBILE:
url = f"{request.endpoint}{request.command}"
elif request.domain == Domain.DEVICES:
elif request.domain == Domain.DEVICES or request.domain == Domain.HOME_BY_BRIDGE:
url = f"{request.endpoint}{request.domain}/{request.device}/{request.command}"
elif request.domain == Domain.ME:
url = f"{request.endpoint}{request.domain}"
elif request.endpoint == Endpoint.MINDER:
params = request.params if request.params is not None else {}

url = (
f"{request.endpoint}{request.domain}/{self._id:d}/{request.command}"
f"?{urlencode(params)}"
)
else:
url = f"{request.endpoint}{request.domain}/{self._id:d}/{request.command}"

if request.params is not None:
params = request.params
url += f"?{urlencode(params)}"

return url

def _configure_payload(self, headers: dict[str, str], request: TadoRequest) -> bytes:
Expand Down
28 changes: 28 additions & 0 deletions PyTado/interface/api/my_tado.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,31 @@ def get_running_times(self, date=datetime.datetime.now().strftime("%Y-%m-%d")) -
request.params = {"from": date}

return self._http.request(request)

def get_boiler_install_state(self, bridge_id: str, auth_key: str):
"""
Get the boiler wiring installation state from home by bridge endpoint
"""

request = TadoRequest()
request.action = Action.GET
request.domain = Domain.HOME_BY_BRIDGE
request.device = bridge_id
request.command = "boilerWiringInstallationState"
request.params = {"authKey": auth_key}

return self._http.request(request)

def get_boiler_max_output_temperature(self, bridge_id: str, auth_key: str):
"""
Get the boiler max output temperature from home by bridge endpoint
"""

request = TadoRequest()
request.action = Action.GET
request.domain = Domain.HOME_BY_BRIDGE
request.device = bridge_id
request.command = "boilerMaxOutputTemperature"
request.params = {"authKey": auth_key}

return self._http.request(request)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"boilerMaxOutputTemperatureInCelsius":50}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"state": "INSTALLATION_COMPLETED",
"deviceWiredToBoiler": {
"type": "RU02B",
"serialNo": "RUXXXXXXXXXX",
"thermInterfaceType": "OPENTHERM",
"connected": true,
"lastRequestTimestamp": "2024-12-28T10:36:47.533Z"
},
"bridgeConnected": true,
"hotWaterZonePresent": false,
"boiler": {
"outputTemperature": {
"celsius": 38.01,
"timestamp": "2024-12-28T10:36:54.000Z"
}
}
}
40 changes: 31 additions & 9 deletions tests/test_my_tado.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class TadoTestCase(unittest.TestCase):

def setUp(self) -> None:
super().setUp()
login_patch = mock.patch(
"PyTado.http.Http._login", return_value=(1, "foo")
)
login_patch = mock.patch("PyTado.http.Http._login", return_value=(1, "foo"))
get_me_patch = mock.patch("PyTado.interface.api.Tado.get_me")
login_patch.start()
get_me_patch.start()
Expand All @@ -35,9 +33,7 @@ def test_home_set_to_manual_mode(
with mock.patch(
"PyTado.http.Http.request",
return_value=json.loads(
common.load_fixture(
"tadov2.home_state.auto_supported.manual_mode.json"
)
common.load_fixture("tadov2.home_state.auto_supported.manual_mode.json")
),
):
self.tado_client.get_home_state()
Expand All @@ -53,9 +49,7 @@ def test_home_already_set_to_auto_mode(
with mock.patch(
"PyTado.http.Http.request",
return_value=json.loads(
common.load_fixture(
"tadov2.home_state.auto_supported.auto_mode.json"
)
common.load_fixture("tadov2.home_state.auto_supported.auto_mode.json")
),
):
self.tado_client.get_home_state()
Expand Down Expand Up @@ -92,3 +86,31 @@ def test_get_running_times(self):
assert self.tado_client._http.request.called
assert running_times["lastUpdated"] == "2023-08-05T19:50:21Z"
assert running_times["runningTimes"][0]["zones"][0]["id"] == 1

def test_get_boiler_install_state(self):
with mock.patch(
"PyTado.http.Http.request",
return_value=json.loads(
common.load_fixture("home_by_bridge.boiler_wiring_installation_state.json")
),
):
boiler_temperature = self.tado_client.get_boiler_install_state(
"IB123456789", "authcode"
)

assert self.tado_client._http.request.called
assert boiler_temperature["boiler"]["outputTemperature"]["celsius"] == 38.01

def test_get_boiler_max_output_temperature(self):
with mock.patch(
"PyTado.http.Http.request",
return_value=json.loads(
common.load_fixture("home_by_bridge.boiler_max_output_temperature.json")
),
):
boiler_temperature = self.tado_client.get_boiler_max_output_temperature(
"IB123456789", "authcode"
)

assert self.tado_client._http.request.called
assert boiler_temperature["boilerMaxOutputTemperatureInCelsius"] == 50.0
Loading