Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Additional support for components #77

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions pysmartthings/device.py
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All methods updating status (like switch_off) needs to take to account component_id in the parameter, because it updates always state of the "main" component, not a correct one. For example if you swith off some component (not the main), it switchs off also main component in device state (it is not really switched off in the smart things device).

Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def apply_data(self, data: dict):
component_id = component["id"]
if component_id == "main":
self._capabilities.extend(capabilities)
else:
self._components[component_id] = capabilities
self._components[component_id] = capabilities

if self._type == DEVICE_TYPE_DTH:
dth = data.get("dth")
Expand Down Expand Up @@ -228,6 +227,13 @@ def values(self) -> Dict[str, Any]:
lambda: None, {k: v.value for k, v in self._attributes.items()}
)

@property
def disabled_components(self) -> []:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from the custom namespace, which means this capability has no formal definition and can be changed by any device implementing it. Meaning, it's not guaranteed to be a dictionary of strings (like you're expecting) and will result in runtime errors if assumed to be so. Given the nature of custom capabilities, they don't below as first class members in this library.

Copy link

@alrassia alrassia Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed your comment on the pr for home assistant, if I understood well the idea is that such functionality is implemented on the ha side and not the library correct?

Copy link
Author

@dwradcliffe dwradcliffe Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my question too; would you accept this moved to ha/core, assuming we check the data type first? Or alternatively check the data type here which would be simpler, IMO.

"""Get the list of disabled components for this device."""
if self._attributes.get("disabledComponents"):
return self._attributes["disabledComponents"].value
return []

@property
def color(self) -> Optional[str]:
"""Get the color attribute."""
Expand Down
26 changes: 26 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ def test_apply_data():
"light",
]
assert device.components == {
"main": [
"switch",
"switchLevel",
"refresh",
"indicator",
"sensor",
"actuator",
"healthCheck",
"light",
],
"bottomButton": ["button"],
"topButton": ["button"],
}
Expand Down Expand Up @@ -1364,6 +1374,22 @@ def test_attributes():
"level": (100, "%", None),
}

@staticmethod
def test_disabled_components():
"""Test the disabled_components property."""
# Arrange
data = get_json("device_status.json")
status = DeviceStatus(None, DEVICE_ID, data)
# Act/Assert
assert status.disabled_components == []
# Arrange
data["components"]["main"]["custom.disabledComponents"] = {
"disabledComponents": {"value": ["SomeDisabledComponent"]}
}
status = DeviceStatus(None, DEVICE_ID, data)
# Act/Assert
assert status.disabled_components == ["SomeDisabledComponent"]

@staticmethod
def test_attributes_default():
"""Test the attributes property."""
Expand Down