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

feat: add query.OUTPUTS to collect outputs status #127

Merged
merged 3 commits into from
Nov 9, 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
17 changes: 14 additions & 3 deletions src/elmo/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ def poll(self, ids):
{
"areas": False,
"inputs": True,
"outputs": False,
"statusAdv": False,
}
"""
payload = {
"sessionId": self._session_id,
"Areas": ids[q.SECTORS],
"Inputs": ids[q.INPUTS],
"Outputs": ids[q.OUTPUTS],
"StatusAdv": ids[q.ALERTS],
"CanElevate": "1",
"ConnectionStatus": "1",
Expand All @@ -133,9 +136,10 @@ def poll(self, ids):
state = response.json()
try:
update = {
"has_changes": state["Areas"] or state["Inputs"] or state["StatusAdv"],
"has_changes": state["Areas"] or state["Inputs"] or state["Outputs"] or state["StatusAdv"],
"areas": state["Areas"],
"inputs": state["Inputs"],
"outputs": state["Outputs"],
"statusadv": state["StatusAdv"],
}
except KeyError as err:
Expand Down Expand Up @@ -499,6 +503,7 @@ def query(self, query):

sectors = client.query(query.SECTORS).get("sectors")
inputs = client.query(query.INPUTS).get("inputs")
outputs = client.query(query.OUTPUTS).get("outputs")

Raises:
QueryNotValid: if the query is not recognized.
Expand Down Expand Up @@ -530,6 +535,7 @@ def query(self, query):
`last_id`: is the last ID of the query, used to retrieve new state changes
`sectors`: is the key you use to retrieve sectors if that was the query
`inputs`: is the key you use to retrieve inputs if that was the query
'outputs`: is the key you use to retrieve outputs if that was the query
"""
# Query detection
if query == q.SECTORS:
Expand All @@ -542,6 +548,11 @@ def query(self, query):
key_group = "inputs"
endpoint = self._router.inputs
_LOGGER.debug("Client | Querying inputs")
elif query == q.OUTPUTS:
status = "Active"
key_group = "outputs"
endpoint = self._router.outputs
_LOGGER.debug("Client | Querying outputs")
elif query == q.ALERTS:
endpoint = self._router.status
_LOGGER.debug("Client | Querying alerts")
Expand All @@ -552,7 +563,7 @@ def query(self, query):
response = self._session.post(endpoint, data={"sessionId": self._session_id})
response.raise_for_status()

if query in [q.SECTORS, q.INPUTS]:
if query in [q.SECTORS, q.INPUTS, q.OUTPUTS]:
# Retrieve description or use the cache
descriptions = self._get_descriptions()

Expand All @@ -572,7 +583,7 @@ def query(self, query):
if entry["InUse"]:
# Address potential data inconsistency between cloud data and main unit.
# In some installations, they may be out of sync, resulting in the cloud
# providing a sector/input that doesn't actually exist in the main unit.
# providing a sector/input/output that doesn't actually exist in the main unit.
# To handle this, we default the name to "Unknown" if its description
# isn't found in the cloud data to prevent KeyError.
name = descriptions[query].get(entry["Index"], "Unknown")
Expand Down
4 changes: 4 additions & 0 deletions src/elmo/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ def sectors(self):
@property
def inputs(self):
return "{}/api/inputs".format(self._base_url)

@property
def outputs(self):
return "{}/api/outputs".format(self._base_url)
1 change: 1 addition & 0 deletions src/elmo/query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SECTORS = 9
INPUTS = 10
ALERTS = 11
OUTPUTS = 12
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def client():
server.add(responses.POST, "https://example.com/api/strings", body=r.STRINGS, status=200)
server.add(responses.POST, "https://example.com/api/areas", body=r.AREAS, status=200)
server.add(responses.POST, "https://example.com/api/inputs", body=r.INPUTS, status=200)
server.add(responses.POST, "https://example.com/api/outputs", body=r.OUTPUTS, status=200)
yield client


Expand Down
78 changes: 78 additions & 0 deletions tests/fixtures/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ def test_client_get_sectors_status(server):
"Description": "Outdoor Sensor 3",
"Created": "/Date(1546004147493+0100)/",
"Version": "AAAAAAAAgRw="
},
{
"AccountId": 1,
"Class": 12,
"Index": 0,
"Description": "Output 1",
"Created": "/Date(1546004147493+0100)/",
"Version": "AAAAAAAAgRw="
},
{
"AccountId": 1,
"Class": 12,
"Index": 1,
"Description": "Output 2",
"Created": "/Date(1546004147493+0100)/",
"Version": "AAAAAAAAgRw="
},
{
"AccountId": 1,
"Class": 12,
"Index": 2,
"Description": "Output 3",
"Created": "/Date(1546004147493+0100)/",
"Version": "AAAAAAAAgRw="
},
{
"AccountId": 3,
"Class": 12,
"Index": 3,
"Description": "Output 4",
"Created": "/Date(1546004147493+0100)/",
"Version": "AAAAAAAAgRw="
}
]"""
AREAS = """[
Expand Down Expand Up @@ -240,3 +272,49 @@ def test_client_get_sectors_status(server):
"InProgress": false
}
]"""
OUTPUTS = """[
{
"Active": true,
"InUse": true,
"DoNotRequireAuthentication": true,
"ControlDeniedToUsers": false,
"Id": 400258,
"Index": 0,
"Element": 1,
"CommandId": 0,
"InProgress": false
},
{
"Active": false,
"InUse": true,
"DoNotRequireAuthentication": false,
"ControlDeniedToUsers": false,
"Id": 400259,
"Index": 1,
"Element": 2,
"CommandId": 0,
"InProgress": false
},
{
"Active": false,
"InUse": true,
"DoNotRequireAuthentication": false,
"ControlDeniedToUsers": false,
"Id": 400260,
"Index": 2,
"Element": 3,
"CommandId": 0,
"InProgress": false
},
{
"Active": false,
"InUse": false,
"DoNotRequireAuthentication": false,
"ControlDeniedToUsers": false,
"Id": 400261,
"Index": 3,
"Element": 4,
"CommandId": 0,
"InProgress": false
}
]"""
Loading