async def test_one_lock_operation_pubnub_connected(hass): """Test lock and unlock operations are async when pubnub is connected.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(hass) assert lock_one.pubsub_channel == "pubsub" pubnub = AugustPubNub() await _create_august_with_devices(hass, [lock_one], pubnub=pubnub) pubnub.connected = True lock_online_with_doorsense_name = hass.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED assert lock_online_with_doorsense_name.attributes.get( "battery_level") == 92 assert (lock_online_with_doorsense_name.attributes.get("friendly_name") == "online_with_doorsense Name") data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} assert await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) await hass.async_block_till_done() pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=(dt_util.utcnow().timestamp() + 1) * 10000000, message={ "status": "kAugLockState_Unlocked", }, ), ) await hass.async_block_till_done() await hass.async_block_till_done() lock_online_with_doorsense_name = hass.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_UNLOCKED assert lock_online_with_doorsense_name.attributes.get( "battery_level") == 92 assert (lock_online_with_doorsense_name.attributes.get("friendly_name") == "online_with_doorsense Name") assert await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) await hass.async_block_till_done() pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=(dt_util.utcnow().timestamp() + 2) * 10000000, message={ "status": "kAugLockState_Locked", }, ), ) await hass.async_block_till_done() await hass.async_block_till_done() lock_online_with_doorsense_name = hass.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED # No activity means it will be unavailable until the activity feed has data entity_registry = er.async_get(hass) lock_operator_sensor = entity_registry.async_get( "sensor.online_with_doorsense_name_operator") assert lock_operator_sensor assert (hass.states.get("sensor.online_with_doorsense_name_operator").state == STATE_UNKNOWN)
async def test_door_sense_update_via_pubnub(hass): """Test creation of a lock with doorsense and bridge.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(hass) assert lock_one.pubsub_channel == "pubsub" pubnub = AugustPubNub() activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") config_entry = await _create_august_with_devices( hass, [lock_one], activities=activities, pubnub=pubnub ) binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={"status": "kAugLockState_Unlocking", "doorState": "closed"}, ), ) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_OFF pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={"status": "kAugLockState_Locking", "doorState": "open"}, ), ) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON pubnub.connected = True async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON # Ensure pubnub status is always preserved async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=2)) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={"status": "kAugLockState_Unlocking", "doorState": "open"}, ), ) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) await hass.async_block_till_done() binary_sensor_online_with_doorsense_name = hass.states.get( "binary_sensor.online_with_doorsense_name_open" ) assert binary_sensor_online_with_doorsense_name.state == STATE_ON await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done()
async def test_lock_update_via_pubnub(opp): """Test creation of a lock with doorsense and bridge.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(opp) assert lock_one.pubsub_channel == "pubsub" pubnub = AugustPubNub() activities = await _mock_activities_from_fixture(opp, "get_activity.lock.json") config_entry = await _create_august_with_devices(opp, [lock_one], activities=activities, pubnub=pubnub) lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={ "status": "kAugLockState_Unlocking", }, ), ) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_UNLOCKED pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={ "status": "kAugLockState_Locking", }, ), ) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED async_fire_time_changed(opp, dt_util.utcnow() + datetime.timedelta(seconds=30)) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED pubnub.connected = True async_fire_time_changed(opp, dt_util.utcnow() + datetime.timedelta(seconds=30)) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED # Ensure pubnub status is always preserved async_fire_time_changed(opp, dt_util.utcnow() + datetime.timedelta(hours=2)) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_LOCKED pubnub.message( pubnub, Mock( channel=lock_one.pubsub_channel, timetoken=dt_util.utcnow().timestamp() * 10000000, message={ "status": "kAugLockState_Unlocking", }, ), ) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_UNLOCKED async_fire_time_changed(opp, dt_util.utcnow() + datetime.timedelta(hours=4)) await opp.async_block_till_done() lock_online_with_doorsense_name = opp.states.get( "lock.online_with_doorsense_name") assert lock_online_with_doorsense_name.state == STATE_UNLOCKED await opp.config_entries.async_unload(config_entry.entry_id) await opp.async_block_till_done()