wyze-sdk
wyze-sdk copied to clipboard
No device group support
Current Wzye Device Groups:
- Camera
- Contact Sensor
- Motion Sensor
- Bulb
- Bulb Color
- Plug
- Outdoor plug
@jslay88 I've been working on this already. Shouldn't be too much longer.
Happy to hear your thoughts on the right way/place to expose these though. I can't decide whether to mix them right into the device type clients like client.bulbs.groups.xxx
or client.bulbs.groups_list
or just allow client.bulbs.turn_on/off
to accept a group_id
OR a device_mac
.
Any thoughts/preferences?
well, you could do a combination of a few things really, and all of it too.
You could have client.bulbs.turn_on/off
have a set of kwargs as you already do, and then determine based off what was passed in to tell you what to do. One of the nice things about Python, is you can pass in a Union of anything on an arg. If for some reason, you get a group_id and a device mac, you could handle them independently as such still, its on them for poor implementation in my opinion, but then at least your method wouldn't be opinionated (see what I did there.)
Could make this really fancy, and handle it all with a single monolithic, yet extremely flexible, function for the device type.
def _ensure_set(self, x: Any) -> Set:
"""
Method used for always returning a set of whatever is passed in
:param Any x: Item to ensure a set
"""
if not isinstance(x, set):
return set(x)
return x
def _turn_on(self, *, device_mac: str, device_model: str,
after: Optional[timedelta] = None, **kwargs) -> WyzeResponse:
"""Turns on a bulb.
:param str device_mac: The device mac. e.g. ``ABCDEF1234567890``
:param str device_model: The device model. e.g. ``WLPA19``
:param Optional[timedelta] after: The delay before performing the action.
:rtype: WyzeResponse
"""
prop_def = DeviceProps.power_state()
if device_model in DeviceModels.MESH_BULB:
if after is not None:
raise WyzeFeatureNotSupportedError("delayed power action")
return super()._api_client().run_action_list(
actions={
"key": "set_mesh_property",
"prop": DeviceProp(definition=PropDef(prop_def.pid, str), value="1"),
"device_mac": device_mac,
"provider_key": device_model,
}
)
if after is None:
return super()._api_client().set_device_property(
mac=device_mac, model=device_model, pid=prop_def.pid, value=1)
return super()._api_client().set_device_timer(mac=device_mac, delay_time=after.seconds,
action_type=1, action_value=1)
def turn_on(self, *,
device: Optional[Union[Iterable[Bulb, MeshBulb], Bulb, MeshBulb]] = None,
device_mac: Optional[Union[Iterable[str], str]] = None,
group: Optional[Union[Iterable[BulbGroup, MeshBulbGroup], BulbGroup, MeshBulbGroup]] = None,
group_id: Optional[Union[Iterable[str], str]] = None,
after: Optional[timedelta] = None, **kwargs) -> Union[Set[WyzeResponse], WyzeResponse]:
"""Turns on a bulb, or set a bulbs.
:param Optional[Union[Iterable[Bulb, MeshBulb], Bulb, MeshBulb]] device: The device object(s)
:param Optional[Union[Iterable[str], str]] device_mac: Device MAC(s)
:param Optional[Union[Iterable[BulbGroup, MeshBulbGroup], BulbGroup, MeshBulbGroup]] group: The device group(s)
:param Optional[Union[Iterable[str], str]] group_id: Group ID(s)
:param Optional[timedelta] after: The delay before performing the action.
:rtype: Union[Set[WyzeResponse], WyzeResponse]
"""
responses = set()
if device:
for _device in self._ensure_set(device):
responses.add(self._turn_on(device_mac=_device.mac, device_model=_device.product.model, after=after))
if device_mac:
for _device in self._ensure_set(device_mac):
_device = self.info(device_mac=_device)
responses.add(self._turn_on(device_mac=_device.mac, device_model=_device.product.model, after=after))
if group:
for _group in self._ensure_set(group):
for _device in _group.bulbs:
responses.add(
self._turn_on(device_mac=_device.mac, device_model=_device.product.model, after=after)
)
if group_id:
for gid in self._ensure_set(group_id):
for _device in self.groups.get(group_id=gid).bulbs:
responses.add(
self._turn_on(device_mac=_device.mac, device_model=_device.product.model, after=after)
)
if len(responses) == 1:
return responses.pop()
return responses