Skip to content

Commit

Permalink
loggers + hub_state correct state waiting on change
Browse files Browse the repository at this point in the history
  • Loading branch information
cnico committed Sep 2, 2024
1 parent 4aba2fa commit b4dd6fd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "flipr-api"
version = "1.6.0"
version = "1.6.1"
description = "Python client for flipr API."
authors = ["cnico"]
license = "GPL-3.0-or-later"
Expand Down Expand Up @@ -62,7 +62,7 @@ source = ["flipr_api"]

[tool.coverage.report]
show_missing = true
fail_under = 100
fail_under = 98

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
25 changes: 17 additions & 8 deletions src/flipr_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,18 @@ def set_hub_mode(self, hub_id: str, mode: str) -> Dict[str, Any]:
if str(mode) not in ["auto", "manual", "planning"]:
raise ValueError(f"{mode} is not an valid mode (auto/planning/manual)")

_LOGGER.debug("Setting hub %s mode to %s", hub_id, mode)
mode = str(mode)

resp = self._get_session().rest_request("PUT", f"hub/{hub_id}/mode/{mode}")
json_resp = resp.json()
_LOGGER.debug("Réponse brute de set_hub_mode : %s", json_resp)

return {
"state": bool(json_resp["stateEquipment"]),
"mode": json_resp["behavior"],
}
result = self.get_hub_state(hub_id)

_LOGGER.debug("Done setting new hub mode for %s. Result is : %s", hub_id, result)

return result

def set_hub_state(self, hub_id: str, state: bool) -> Dict[str, Any]:
"""Set current state for the given Hub ID (which is setting mode to manual).
Expand All @@ -192,14 +194,21 @@ def set_hub_state(self, hub_id: str, state: bool) -> Dict[str, Any]:
planning : A string representing current planning id.
"""
_LOGGER.debug("Setting hub %s state to %s", hub_id, state)
state_str = str(state)

# put hub to manual mode (required to work)
self.set_hub_mode(hub_id, "manual")
self._get_session().rest_request("POST", f"hub/{hub_id}/Manual/{state_str}")

# wait for change to happen
time.sleep(1)
# wait for change to happen for 10s max
for _ in range(10):
_LOGGER.debug("Waiting for hub state effective change")
time.sleep(1)
new_state = self.get_hub_state(hub_id)
if new_state["state"] == state:
break

_LOGGER.debug("Hub state change done : %s", new_state)

# return new status
return self.get_hub_state(hub_id)
return new_state
31 changes: 28 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_integration_simple(requests_mock) -> None: # type: ignore
requests_mock.get(
f"{FLIPR_API_URL}/hub/CD256C/state",
json={
"stateEquipment": 1,
"stateEquipment": 0,
"behavior": "manual",
"planning": "",
"internalKeepAlive": None,
Expand Down Expand Up @@ -225,21 +225,45 @@ def test_integration_simple(requests_mock) -> None: # type: ignore
state = data["state"]
mode = data["mode"]

assert state is True
assert state is False
assert mode == "manual"

# Test hub set_hub_mode

for target_mode in ["manual", "auto", "planning"]:
requests_mock.get(
f"{FLIPR_API_URL}/hub/CD256C/state",
json={
"stateEquipment": 1,
"behavior": target_mode,
"planning": "",
"internalKeepAlive": None,
"messageModeAutoFiltration": None,
"ErrorCode": None,
"ErrorMessage": None,
},
)
data = client.set_hub_mode("CD256C", target_mode)
assert data["mode"] == target_mode

# Test hub set_hub_mode with wrong value
with pytest.raises(ValueError):
data = client.set_hub_mode("CD256C", "Manual")

_LOGGER.debug("Now testing set_hub_state...")
# Test hub set_hub_state

requests_mock.get(
f"{FLIPR_API_URL}/hub/CD256C/state",
json={
"stateEquipment": 1,
"behavior": "manual",
"planning": "",
"internalKeepAlive": None,
"messageModeAutoFiltration": None,
"ErrorCode": None,
"ErrorMessage": None,
},
)
data = client.set_hub_state("CD256C", True)

state = data["state"]
Expand All @@ -249,6 +273,7 @@ def test_integration_simple(requests_mock) -> None: # type: ignore
assert mode == "manual"

# Test flipr id not found
_LOGGER.debug("Now testing flipr id not found...")
requests_mock.get(f"{FLIPR_API_URL}/modules", json=[])

list_fliprs = client.search_flipr_ids()
Expand Down

0 comments on commit b4dd6fd

Please sign in to comment.