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_set_lock_state(self):
mock_controller = mock.create_autospec(pyvera.VeraController)
status_json = json.loads(
'{'
' "id": 33,'
' "deviceInfo": {'
# pyvera.CATEGORY_LOCK
' "category": 7,'
' "categoryName": "Doorlock",'
' "name": "MyTestDeadbolt",'
' "locked": 0'
' }'
'}'
)
lock = pyvera.VeraLock(status_json, [], mock_controller)
lock.set_lock_state(1)
self.assertTrue(lock.get_value('locked'), '1')
def test_lock(vera_controller_data: VeraControllerData) -> None:
"""Test function."""
controller = vera_controller_data.controller
device = cast(VeraLock, controller.get_device_by_id(DEVICE_LOCK_ID))
controller.register(device, lambda device: None)
assert device.is_locked() is False
device.lock()
assert device.is_locked() is True
device.unlock()
assert device.is_locked() is False
def test__event_device_for_vera_lock_status():
"""Test function."""
registry = pyvera.SubscriptionRegistry()
mock_lock = mock.create_autospec(pyvera.VeraLock)
mock_lock.name = mock.MagicMock(return_value="MyTestDeadbolt")
# Deadbolt changing but not done
device_json = json.loads(
"{"
# subscribe.STATE_JOB_IN_PROGRESS
' "state": "1"'
"}"
)
registry._event_device(mock_lock, device_json, [])
mock_lock.update.assert_not_called()
# Deadbolt progress with reset state but not done
device_json = json.loads(
"{"
# subscribe.STATE_NO_JOB
def test__event_device_for_vera_lock_status() -> None:
"""Test function."""
registry = SubscriptionRegistry()
registry.set_controller(MagicMock(spec=VeraController))
mock_lock = MagicMock(spec=VeraLock)
mock_lock.name = MagicMock(return_value="MyTestDeadbolt")
# Deadbolt changing but not done
device_json: dict = {"state": STATE_JOB_IN_PROGRESS}
registry._event_device(mock_lock, device_json, [])
mock_lock.update.assert_not_called()
# Deadbolt progress with reset state but not done
device_json = {
"state": STATE_NO_JOB,
"comment": "MyTestDeadbolt: Sending the Z-Wave command after 0 retries",
}
registry._event_device(mock_lock, device_json, [])
mock_lock.update.assert_not_called()
# Deadbolt progress locked but not done
# Define a callback that runs each time a device changes state
def device_info_callback(vera_device):
# Do what we want with the changed device information
print('{}_{}: locked={}'.format(vera_device.name, vera_device.device_id, vera_device.is_locked()))
# Start the controller
controller, _ = pyvera.init_controller(args.url)
try:
# Get a list of all the devices on the vera controller
all_devices = controller.get_devices()
# Look over the list and find the lock devices
lock_devices = []
for device in all_devices:
if isinstance(device, pyvera.VeraLock):
# Register a callback that runs when the info for that device is updated
controller.register(device, device_info_callback)
print('Initially, {}_{}: locked={}'.format(device.name, device.device_id, device.is_locked()))
lock_devices.append(device)
if not device.is_locked():
device.lock()
# Loop until someone hits Ctrl-C to interrupt the listener
try:
all_locked = False
while not all_locked:
time.sleep(1)
all_locked = True
for device in lock_devices:
if not device.is_locked():
all_locked = False
parser.add_argument(
"-u", "--url", help="Vera URL, e.g. http://192.168.1.161:3480", required=True
)
args = parser.parse_args()
# Start the controller
controller = VeraController(args.url)
controller.start()
try:
# Get a list of all the devices on the vera controller
all_devices = controller.get_devices()
# Look over the list and find the lock devices
for device in all_devices:
if isinstance(device, VeraLock):
print(
"{} {} ({})".format(
type(device).__name__, device.name, device.device_id
)
)
print(" comm_failure: {}".format(device.comm_failure))
print(" room_id: {}".format(device.room_id))
print(" is_locked(): {}".format(device.is_locked()))
print(" get_pin_failed(): {}".format(device.get_pin_failed()))
print(" get_unauth_user(): {}".format(device.get_unauth_user()))
print(" get_lock_failed(): {}".format(device.get_lock_failed()))
print(" get_last_user(): {}".format(device.get_last_user()))
print(" get_pin_codes(): {}".format(device.get_pin_codes()))
finally:
# Stop the subscription listening thread so we can quit
def map_vera_device(vera_device, remap):
"""Map vera classes to Home Assistant types."""
import pyvera as veraApi
if isinstance(vera_device, veraApi.VeraDimmer):
return 'light'
if isinstance(vera_device, veraApi.VeraBinarySensor):
return 'binary_sensor'
if isinstance(vera_device, veraApi.VeraSensor):
return 'sensor'
if isinstance(vera_device, veraApi.VeraArmableDevice):
return 'switch'
if isinstance(vera_device, veraApi.VeraLock):
return 'lock'
if isinstance(vera_device, veraApi.VeraThermostat):
return 'climate'
if isinstance(vera_device, veraApi.VeraCurtain):
return 'cover'
if isinstance(vera_device, veraApi.VeraSceneController):
return 'sensor'
if isinstance(vera_device, veraApi.VeraSwitch):
if vera_device.device_id in remap:
return 'light'
return 'switch'
return None
def map_vera_device(vera_device, remap):
"""Map vera classes to Home Assistant types."""
import pyvera as veraApi
if isinstance(vera_device, veraApi.VeraDimmer):
return "light"
if isinstance(vera_device, veraApi.VeraBinarySensor):
return "binary_sensor"
if isinstance(vera_device, veraApi.VeraSensor):
return "sensor"
if isinstance(vera_device, veraApi.VeraArmableDevice):
return "switch"
if isinstance(vera_device, veraApi.VeraLock):
return "lock"
if isinstance(vera_device, veraApi.VeraThermostat):
return "climate"
if isinstance(vera_device, veraApi.VeraCurtain):
return "cover"
if isinstance(vera_device, veraApi.VeraSceneController):
return "sensor"
if isinstance(vera_device, veraApi.VeraSwitch):
if vera_device.device_id in remap:
return "light"
return "switch"
return None