Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init():
"""Test the initialization method."""
# Arrange/Act
sub = Subscription()
# Assert
assert sub.source_type is SourceType.UNKNOWN
assert sub.capability == '*'
assert sub.attribute == '*'
assert sub.value == '*'
assert sub.state_change_only
def test_apply_data_device():
"""Test apply data."""
# Arrange
data = get_json('subscription_device_get_response.json')
sub = Subscription()
# Act
sub.apply_data(data)
# Assert
assert sub.subscription_id == '498752fd-db87-4a5e-95f5-25a0e412838d'
assert sub.installed_app_id == INSTALLED_APP_ID
assert sub.source_type == SourceType.DEVICE
assert sub.device_id == '64e7f664-5b99-4573-b76d-03be3021dc78'
assert sub.component_id == '*'
assert sub.capability == '*'
assert sub.attribute == '*'
assert sub.value == '*'
assert sub.state_change_only
assert not sub.subscription_name
def test_to_data_capability():
"""Test the to_data method for capabilities."""
# Arrange
sub = Subscription()
sub.source_type = SourceType.CAPABILITY
sub.location_id = '397678e5-9995-4a39-9d9f-ae6ba310236b'
sub.capability = 'switch'
sub.attribute = 'switchLevel'
sub.value = '100'
sub.state_change_only = False
sub.subscription_name = "Test"
# Act
data = sub.to_data()
# Assert
assert data['sourceType'] == SourceType.CAPABILITY.value
assert data['capability']['locationId'] == \
'397678e5-9995-4a39-9d9f-ae6ba310236b'
assert data['capability']['capability'] == 'switch'
assert data['capability']['attribute'] == 'switchLevel'
assert data['capability']['value'] == '100'
def test_to_data_device():
"""Test the to_data method for devices."""
# Arrange
sub = Subscription()
sub.source_type = SourceType.DEVICE
sub.device_id = '397678e5-9995-4a39-9d9f-ae6ba310236b'
sub.component_id = 'main'
sub.capability = 'switch'
sub.attribute = 'switchLevel'
sub.value = '100'
sub.state_change_only = True
sub.subscription_name = "Test"
# Act
data = sub.to_data()
# Assert
assert data['sourceType'] == SourceType.DEVICE.value
assert data['device']['deviceId'] == \
'397678e5-9995-4a39-9d9f-ae6ba310236b'
assert data['device']['componentId'] == 'main'
assert data['device']['capability'] == "switch"
def test_apply_data_capability():
"""Test apply data."""
# Arrange
data = get_json('subscription_capability_get_response.json')
sub = Subscription()
# Act
sub.apply_data(data)
# Assert
assert sub.subscription_id == '7bdf5909-57c4-41f3-9089-e520513bd92a'
assert sub.installed_app_id == INSTALLED_APP_ID
assert sub.source_type == SourceType.CAPABILITY
assert sub.location_id == '397678e5-9995-4a39-9d9f-ae6ba310236b'
assert sub.capability == 'switchLevel'
assert sub.attribute == '*'
assert sub.value == '*'
assert sub.state_change_only
assert sub.subscription_name == 'switchLevel_sub'
async def test_create_subscription(smartthings):
"""Tests the create subscription method."""
# Arrange
sub = Subscription()
sub.source_type = 'CAPABILITY'
sub.location_id = LOCATION_ID
sub.capability = 'switch'
sub.installed_app_id = INSTALLED_APP_ID
# Act
entity = await smartthings.create_subscription(sub)
# Assert
assert entity.subscription_id == SUBSCRIPTION_ID
def __init__(self, api: Api, data: Optional[dict] = None):
"""Create a new instance of the SubscriptionEntity class."""
Entity.__init__(self, api)
Subscription.__init__(self)
if data:
self.apply_data(data)
def device_id(self, value: str):
"""Set the GUID of the device that is subscribed to."""
self._device_id = value
@property
def component_id(self) -> str:
"""Get the component ID on the device that is subscribed to."""
return self._component_id
@component_id.setter
def component_id(self, value: str):
"""Set the component ID on the device that is subscribed to."""
self._component_id = value
class SubscriptionEntity(Entity, Subscription):
"""Define a subscription entity."""
def __init__(self, api: Api, data: Optional[dict] = None):
"""Create a new instance of the SubscriptionEntity class."""
Entity.__init__(self, api)
Subscription.__init__(self)
if data:
self.apply_data(data)
async def refresh(self):
"""Refresh the subscription information using the API."""
data = await self._api.get_subscription(
self._installed_app_id, self._subscription_id
)
self.apply_data(data)