Skip to content

Commit

Permalink
Merge pull request #10 from robbinjanssen/rja/add-duration
Browse files Browse the repository at this point in the history
Add support for setting duration when using comfort mode
  • Loading branch information
robbinjanssen committed Jan 28, 2023
2 parents 483f249 + 34de3a5 commit f161961
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions ojmicroline_thermostat/const.py
Expand Up @@ -12,3 +12,4 @@
REGULATION_ECO = 9

DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
COMFORT_DURATION = 240
15 changes: 13 additions & 2 deletions ojmicroline_thermostat/ojmicroline.py
Expand Up @@ -12,6 +12,7 @@
from aiohttp import ClientError, ClientSession, hdrs
from yarl import URL

from .const import COMFORT_DURATION
from .exceptions import (
OJMicrolineAuthException,
OJMicrolineConnectionException,
Expand Down Expand Up @@ -207,7 +208,11 @@ async def get_thermostats(self) -> list[Thermostat]:
return results

async def set_regulation_mode(
self, resource: Thermostat, regulation_mode: int, temperature: int | None = None
self,
resource: Thermostat,
regulation_mode: int,
temperature: int | None = None,
duration: int = COMFORT_DURATION,
) -> bool:
"""
Set the regulation mode.
Expand All @@ -221,6 +226,8 @@ async def set_regulation_mode(
the thermostat to update.
regulation_mode: The mode to set the thermostat to.
temperature: The temperature to set or None.
duration: The duration in minutes to set the temperature
for (comfort mode only), defaults to 4 hours.
Returns:
True if it succeeded.
Expand All @@ -236,7 +243,11 @@ async def set_regulation_mode(
"api/Group/UpdateGroup",
method=hdrs.METH_POST,
params={"sessionid": self.__session_id},
body=request.update_regulation_mode(regulation_mode, temperature),
body=request.update_regulation_mode(
regulation_mode=regulation_mode,
temperature=temperature,
duration=duration,
),
)

if data["ErrorCode"] == 1:
Expand Down
20 changes: 12 additions & 8 deletions ojmicroline_thermostat/requests/update_group_request.py
Expand Up @@ -6,6 +6,7 @@
from typing import Any

from ..const import (
COMFORT_DURATION,
DATETIME_FORMAT,
REGULATION_BOOST,
REGULATION_COMFORT,
Expand Down Expand Up @@ -36,14 +37,19 @@ def __init__(self, resource: Thermostat, api_key: str):
self.api_key = api_key

def update_regulation_mode(
self, regulation_mode: int, temperature: int | None = None
self,
regulation_mode: int,
temperature: int | None = None,
duration: int = COMFORT_DURATION,
) -> dict[str, Any]:
"""
Return a request body to update the regulation mode for a group.
Args:
regulation_mode: The mode to set the regulation mode in.
temperature: The temperature (for comfort and manual mode) or None.
duration: The duration in minutes to set the temperature
for (comfort mode only), defaults to 4 hours.
Returns:
A JSON object containing the update.
Expand All @@ -52,7 +58,7 @@ def update_regulation_mode(
self.resource.temperatures[regulation_mode] = temperature

if regulation_mode == REGULATION_COMFORT:
comfort_end_time = datetime.today() + timedelta(hours=4)
comfort_end_time = datetime.today() + timedelta(minutes=duration)
else:
comfort_end_time = self.resource.comfort_end_time

Expand All @@ -72,19 +78,17 @@ def update_regulation_mode(
"ComfortSetpoint": self.resource.temperatures[REGULATION_COMFORT],
"LastPrimaryModeIsAuto": (
self.resource.regulation_mode == REGULATION_SCHEDULE
), # noqa: E501
),
"ManualModeSetpoint": self.resource.temperatures[REGULATION_MANUAL],
"RegulationMode": regulation_mode,
"Schedule": self.resource.schedule,
"VacationEnabled": self.resource.vacation_mode,
"VacationBeginDay": self.resource.vacation_begin_time.strftime(
DATETIME_FORMAT
), # noqa: E501
),
"VacationEndDay": self.resource.vacation_end_time.strftime(
DATETIME_FORMAT
), # noqa: E501
"VacationTemperature": self.resource.temperatures[
REGULATION_VACATION
], # noqa: E501
),
"VacationTemperature": self.resource.temperatures[REGULATION_VACATION],
},
}
4 changes: 2 additions & 2 deletions tests/test_ojmicroline.py
Expand Up @@ -18,7 +18,7 @@
OJMicrolineTimeoutException,
Thermostat,
)
from ojmicroline_thermostat.const import REGULATION_MANUAL
from ojmicroline_thermostat.const import REGULATION_COMFORT, REGULATION_MANUAL

from . import load_fixtures

Expand Down Expand Up @@ -385,6 +385,6 @@ async def test_set_regulation_mode_failed(
monkeypatch.setattr(client, "_OJMicroline__session_id", "f00b4r")

with pytest.raises(OJMicrolineException):
await client.set_regulation_mode(thermostat, REGULATION_MANUAL, 2500)
await client.set_regulation_mode(thermostat, REGULATION_COMFORT, 2500, 360)

assert client._OJMicroline__session_calls_left == 299
47 changes: 44 additions & 3 deletions tests/test_request_update_group.py
Expand Up @@ -6,6 +6,7 @@
from freezegun import freeze_time

from ojmicroline_thermostat.const import (
COMFORT_DURATION,
REGULATION_BOOST,
REGULATION_COMFORT,
REGULATION_MANUAL,
Expand Down Expand Up @@ -50,11 +51,11 @@ async def test_update_regulation_mode_comfort() -> None:
assert thermostat.temperatures[REGULATION_COMFORT] == 2000

request = UpdateGroupRequest(resource=thermostat, api_key="v3ry-s3cr3t")
result = request.update_regulation_mode(REGULATION_COMFORT)
result = request.update_regulation_mode(regulation_mode=REGULATION_COMFORT)

# Assert comfort end time is the current date + 4 hours.
# Assert comfort end time is the current date + COMFORT_DURATION minutes.
assert result["SetGroup"]["ComfortEndTime"] == (
datetime.now() + timedelta(hours=4)
datetime.now() + timedelta(minutes=COMFORT_DURATION)
).strftime(dateformat)

# Assert rest is the same.
Expand All @@ -66,6 +67,46 @@ async def test_update_regulation_mode_comfort() -> None:
assert result["SetGroup"]["ComfortSetpoint"] == 2000


@pytest.mark.asyncio
@freeze_time("2023-01-01 11:30:35")
async def test_update_regulation_mode_comfort_with_temp_and_duration() -> None:
"""
Test that the regulation mode can be set to comfort.
Make sure the end times are set correctly.
"""
data = load_fixtures("single_thermostat.json")
thermostat = Thermostat.from_json(json.loads(data))
dateformat = "%Y-%m-%dT%H:%M:%S"

# Check the current times.
assert thermostat.comfort_end_time.strftime(dateformat) == "2022-12-28T15:58:34"
assert thermostat.boost_end_time.strftime(dateformat) == "2023-01-02T14:55:01"
assert thermostat.vacation_begin_time.strftime(dateformat) == "2022-12-21T00:00:00"
assert thermostat.vacation_end_time.strftime(dateformat) == "2022-12-22T00:00:00"

# Check the current temperature for comfort.
assert thermostat.temperatures[REGULATION_COMFORT] == 2000

request = UpdateGroupRequest(resource=thermostat, api_key="v3ry-s3cr3t")
result = request.update_regulation_mode(
regulation_mode=REGULATION_COMFORT, temperature=2350, duration=360
)

# Assert comfort end time is the current date + 360 minutes.
assert result["SetGroup"]["ComfortEndTime"] == (
datetime.now() + timedelta(minutes=360)
).strftime(dateformat)

# Assert rest is the same.
assert result["SetGroup"]["BoostEndTime"] == "2023-01-02T14:55:01"
assert result["SetGroup"]["VacationBeginDay"] == "2022-12-21T00:00:00"
assert result["SetGroup"]["VacationEndDay"] == "2022-12-22T00:00:00"

# Assert the temperature is the same.
assert result["SetGroup"]["ComfortSetpoint"] == 2350


@pytest.mark.asyncio
@freeze_time("2023-01-01 11:30:35")
async def test_update_regulation_mode_boost() -> None:
Expand Down

0 comments on commit f161961

Please sign in to comment.