Skip to content

Commit

Permalink
feat: add outputs status and command
Browse files Browse the repository at this point in the history
  • Loading branch information
xtimmy86x committed Nov 30, 2023
1 parent 27838de commit 8531d89
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
90 changes: 49 additions & 41 deletions custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,12 @@ def disarm(self, code, sectors=None):
_LOGGER.error(f"Device | Credentials (alarm code) is incorrect: {err}")
raise err

def turn_off(self, outputs=None):
def turn_off(self, output):
"""
Turn off a specified output.
Args:
outputs (str): The ID of the output to be turned off.
output: The ID of the output to be turned off.
Raises:
HTTPError: If there is an error in the HTTP request to turn off the output.
Expand All @@ -323,33 +323,38 @@ def turn_off(self, outputs=None):
Example:
To turn off an output with ID '1', use:
>>> device_instance.turn_off(outputs='1')
>>> device_instance.turn_off(1)
"""
for id, item in self.items(q.OUTPUTS):
if id == outputs:
if not item.get("control_denied_to_users"):
if item.get("do_not_require_authentication"):
element_id = item.get("element")
try:
self._connection.turn_off(element_id)
except HTTPError as err:
_LOGGER.error(f"Device | Error while turning off outputs: {err.response.text}")
raise err
else:
_LOGGER.error(
f"Device | Error while turning off output: {item.get('name')}, Required authentication"
)
else:
_LOGGER.error(
f"Device | Error while turning off output: {item.get('name')}, Can't be manual controlled"
)

def turn_on(self, outputs=None):
# Skip if it's not matching the output
if id != output:
continue

# If the output isn't manual controllable by users write an error il log
if item.get("control_denied_to_users"):
_LOGGER.error(
f"Device | Error while turning off output: {item.get('name')}, Can't be manual controlled"
)
break

# If the output require authentication for control write an error il log
if not item.get("do_not_require_authentication"):
_LOGGER.error(f"Device | Error while turning off output: {item.get('name')}, Required authentication")
break

try:
element_id = item.get("element")
self._connection.turn_off(element_id)
except HTTPError as err:
_LOGGER.error(f"Device | Error while turning off output: {err.response.text}")
raise err

def turn_on(self, output):
"""
Turn on a specified output.
Args:
outputs (str): The ID of the output to be turned on.
output: The ID of the output to be turned on.
Raises:
HTTPError: If there is an error in the HTTP request to turn on the output.
Expand All @@ -363,23 +368,26 @@ def turn_on(self, outputs=None):
Example:
To turn on an output with ID '1', use:
>>> device_instance.turn_on(outputs='1')
>>> device_instance.turn_on(1)
"""
for id, item in self.items(q.OUTPUTS):
if id == outputs:
if not item.get("control_denied_to_users"):
if item.get("do_not_require_authentication"):
element_id = item.get("element")
try:
self._connection.turn_on(element_id)
except HTTPError as err:
_LOGGER.error(f"Device | Error while turning on outputs: {err.response.text}")
raise err
else:
_LOGGER.error(
f"Device | Error while turning on output: {item.get('name')}, Required authentication"
)
else:
_LOGGER.error(
f"Device | Error while turning on output: {item.get('name')}, Can't be manual controlled"
)
# Skip if it's not matching the output
if id != output:
continue

# If the output isn't manual controllable by users write an error log
if item.get("control_denied_to_users"):
_LOGGER.error(f"Device | Error while turning on output: {item.get('name')}, Can't be manual controlled")
break

# If the output require authentication for control write an error log
if not item.get("do_not_require_authentication"):
_LOGGER.error(f"Device | Error while turning on output: {item.get('name')}, Required authentication")
break

try:
element_id = item.get("element")
self._connection.turn_on(element_id)
except HTTPError as err:
_LOGGER.error(f"Device | Error while turning on outputs: {err.response.text}")
raise err
24 changes: 12 additions & 12 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,26 +715,26 @@ def test_device_update_state_machine_armed(client, mocker):
"id": 1,
"index": 0,
"element": 1,
"controldeniedtousers": False,
"donotrequireauthentication": True,
"control_denied_to_users": False,
"do_not_require_authentication": True,
"status": True,
"name": "Output 1",
},
1: {
"id": 2,
"index": 1,
"element": 2,
"controldeniedtousers": False,
"donotrequireauthentication": False,
"control_denied_to_users": False,
"do_not_require_authentication": False,
"status": True,
"name": "Output 2",
},
2: {
"id": 3,
"index": 2,
"element": 3,
"controldeniedtousers": True,
"donotrequireauthentication": False,
"control_denied_to_users": True,
"do_not_require_authentication": False,
"status": False,
"name": "Output 3",
},
Expand Down Expand Up @@ -805,26 +805,26 @@ def test_device_update_state_machine_disarmed(client, mocker):
"id": 1,
"index": 0,
"element": 1,
"controldeniedtousers": False,
"donotrequireauthentication": True,
"control_denied_to_users": False,
"do_not_require_authentication": True,
"status": True,
"name": "Output 1",
},
1: {
"id": 2,
"index": 1,
"element": 2,
"controldeniedtousers": False,
"donotrequireauthentication": False,
"control_denied_to_users": False,
"do_not_require_authentication": False,
"status": True,
"name": "Output 2",
},
2: {
"id": 3,
"index": 2,
"element": 3,
"controldeniedtousers": True,
"donotrequireauthentication": False,
"control_denied_to_users": True,
"do_not_require_authentication": False,
"status": False,
"name": "Output 3",
},
Expand Down

0 comments on commit 8531d89

Please sign in to comment.