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

Fetch more data in get_user() and get_controllers() #74

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
185 changes: 93 additions & 92 deletions pydrawise/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,26 @@ async def get_user(self) -> User:
:rtype: User
"""
resp_json = await self._get("customerdetails.php")
return User(
user = User(
id=0,
customer_id=resp_json["customer_id"],
name="",
email="",
controllers=[],
controllers=[_controller_from_json(c) for c in resp_json["controllers"]],
)
for controller in user.controllers:
controller.zones = await self.get_zones(controller)
return user

async def get_controllers(self) -> list[Controller]:
"""Retrieves all controllers associated with the currently authenticated user.

:rtype: list[Controller]
"""
resp_json = await self._get("customerdetails.php", type="controllers")
controllers = []
for controller_json in resp_json["controllers"]:
controllers.append(
Controller(
id=controller_json["controller_id"],
name=controller_json["name"],
software_version="",
hardware=ControllerHardware(
serial_number=controller_json["serial_number"],
version="",
status="",
model=ControllerModel(
name="",
description="",
),
firmware=[],
),
last_contact_time=datetime.fromtimestamp(
controller_json["last_contact"]
),
last_action="",
online=True,
sensors=[],
zones=[],
permitted_program_start_times=[],
status=None,
)
)
controllers = [_controller_from_json(c) for c in resp_json["controllers"]]
for controller in controllers:
controller.zones = await self.get_zones(controller)
return controllers

async def get_controller(self, controller_id: int) -> Controller:
Expand All @@ -114,68 +92,7 @@ async def get_zones(self, controller: Controller) -> list[Zone]:
"""
_ = controller # unused
resp_json = await self._get("statusschedule.php")
zones = []
for zone_json in resp_json["relays"]:
current_run = None
next_run = None
if zone_json["time"] == 1:
# in progress
current_run = ScheduledZoneRun(
id="",
start_time=datetime.now(),
end_time=datetime.now(),
normal_duration=timedelta(minutes=0),
duration=timedelta(minutes=0),
remaining_time=timedelta(seconds=zone_json["run"]),
status=RunStatus(value=0, label=""),
)
else:
start_time = datetime.now() + timedelta(seconds=zone_json["time"])
duration = timedelta(seconds=zone_json["run"])
next_run = ScheduledZoneRun(
id="",
start_time=start_time,
end_time=start_time + duration,
normal_duration=duration,
duration=duration,
remaining_time=timedelta(seconds=0),
status=RunStatus(value=0, label=""),
)
suspended_until = None
if zone_json["time"] == 1576800000:
suspended_until = ZoneSuspension(
id=0,
start_time=datetime.now(),
end_time=datetime.now() + timedelta(seconds=zone_json["time"]),
)
zones.append(
Zone(
id=zone_json["relay_id"],
number=zone_json["relay"],
name=zone_json["name"],
watering_settings=StandardWateringSettings(
fixed_watering_adjustment=0,
cycle_and_soak_settings=None,
standard_program_applications=[],
),
scheduled_runs=ScheduledZoneRuns(
summary="",
current_run=current_run,
next_run=next_run,
status="",
),
past_runs=PastZoneRuns(
last_run=None,
runs=[],
),
status=ZoneStatus(
relative_water_balance=0,
suspended_until=suspended_until,
),
suspensions=[],
)
)
return zones
return [_zone_from_json(z) for z in resp_json["relays"]]

async def get_zone(self, zone_id: int) -> Zone:
"""Retrieves a zone by its unique identifier.
Expand Down Expand Up @@ -301,6 +218,90 @@ async def delete_zone_suspension(self, suspension: ZoneSuspension) -> None:
raise NotImplementedError


def _controller_from_json(controller_json: dict) -> Controller:
return Controller(
id=controller_json["controller_id"],
name=controller_json["name"],
software_version="",
hardware=ControllerHardware(
serial_number=controller_json["serial_number"],
version="",
status="",
model=ControllerModel(
name="",
description="",
),
firmware=[],
),
last_contact_time=datetime.fromtimestamp(controller_json["last_contact"]),
last_action="",
online=True,
sensors=[],
zones=[],
permitted_program_start_times=[],
status=None,
)


def _zone_from_json(zone_json: dict) -> Zone:
current_run = None
next_run = None
if zone_json["time"] == 1:
current_run = ScheduledZoneRun(
id="",
start_time=datetime.now(),
end_time=datetime.now(),
normal_duration=timedelta(minutes=0),
duration=timedelta(minutes=0),
remaining_time=timedelta(seconds=zone_json["run"]),
status=RunStatus(value=0, label=""),
)
else:
start_time = datetime.now() + timedelta(seconds=zone_json["time"])
duration = timedelta(seconds=zone_json["run"])
next_run = ScheduledZoneRun(
id="",
start_time=start_time,
end_time=start_time + duration,
normal_duration=duration,
duration=duration,
remaining_time=timedelta(seconds=0),
status=RunStatus(value=0, label=""),
)
suspended_until = None
if zone_json["time"] == 1576800000:
suspended_until = ZoneSuspension(
id=0,
start_time=datetime.now(),
end_time=datetime.now() + timedelta(seconds=zone_json["time"]),
)
return Zone(
id=zone_json["relay_id"],
number=zone_json["relay"],
name=zone_json["name"],
watering_settings=StandardWateringSettings(
fixed_watering_adjustment=0,
cycle_and_soak_settings=None,
standard_program_applications=[],
),
scheduled_runs=ScheduledZoneRuns(
summary="",
current_run=current_run,
next_run=next_run,
status="",
),
past_runs=PastZoneRuns(
last_run=None,
runs=[],
),
status=ZoneStatus(
relative_water_balance=0,
suspended_until=suspended_until,
),
suspensions=[],
)


class LegacyHydrawise:
"""Client library for interacting with Hydrawise v1 API.

Expand Down
95 changes: 61 additions & 34 deletions tests/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,45 +186,72 @@ def mock_request(customer_details, status_schedule):
class TestLegacyHydrawiseAsync:
"""Test the LegacyHydrawiseAsync class."""

async def test_get_user(self, customer_details: dict) -> None:
async def test_get_user(
self, customer_details: dict, status_schedule: dict
) -> None:
"""Test the get_user method."""
client = legacy.LegacyHydrawiseAsync(API_KEY)
with aioresponses() as m:
m.get(
re.compile("https://api.hydrawise.com/api/v1/customerdetails.php"),
status=200,
payload=customer_details,
)
user = await client.get_user()
m.assert_called_once_with(
"https://api.hydrawise.com/api/v1/customerdetails.php",
method="GET",
params={"api_key": API_KEY},
timeout=10,
)
assert user.customer_id == 47076
with freeze_time("2023-01-01 01:00:00") as t:
with aioresponses() as m:
m.get(
re.compile("https://api.hydrawise.com/api/v1/customerdetails.php"),
status=200,
payload=customer_details,
)
m.get(
re.compile("https://api.hydrawise.com/api/v1/statusschedule.php"),
status=200,
payload=status_schedule,
)
user = await client.get_user()
assert user.customer_id == 47076
assert [c.id for c in user.controllers] == [52496]
assert [z.id for z in user.controllers[0].zones] == [
5965394,
5965395,
5965396,
5965397,
5965398,
5965399,
5965400,
5965401,
5965402,
]

async def test_get_controllers(self, customer_details: dict) -> None:
async def test_get_controllers(
self, customer_details: dict, status_schedule: dict
) -> None:
"""Test the get_controllers method."""
client = legacy.LegacyHydrawiseAsync(API_KEY)
with aioresponses() as m:
m.get(
re.compile("https://api.hydrawise.com/api/v1/customerdetails.php"),
status=200,
payload=customer_details,
)
controllers = await client.get_controllers()
m.assert_called_once_with(
"https://api.hydrawise.com/api/v1/customerdetails.php",
method="GET",
params={"api_key": API_KEY, "type": "controllers"},
timeout=10,
)
[controller] = controllers
assert controller.id == 52496
assert controller.name == "Home Controller"
assert controller.hardware.serial_number == "0310b36090"
assert controller.last_contact_time == datetime(2023, 8, 29, 7, 0, 20)
with freeze_time("2023-01-01 01:00:00") as t:
with aioresponses() as m:
m.get(
re.compile("https://api.hydrawise.com/api/v1/customerdetails.php"),
status=200,
payload=customer_details,
)
m.get(
re.compile("https://api.hydrawise.com/api/v1/statusschedule.php"),
status=200,
payload=status_schedule,
)
controllers = await client.get_controllers()
[controller] = controllers
assert controller.id == 52496
assert controller.name == "Home Controller"
assert controller.hardware.serial_number == "0310b36090"
assert controller.last_contact_time == datetime(2023, 8, 29, 7, 0, 20)
assert [z.id for z in controller.zones] == [
5965394,
5965395,
5965396,
5965397,
5965398,
5965399,
5965400,
5965401,
5965402,
]

async def test_get_zones(self, status_schedule: dict) -> None:
"""Test the get_zones method."""
Expand Down