async def test_event_handler_fires_button_events(hass, config_entry, device_factory, event_factory, event_request_factory): """Test the event handler fires button events.""" device = device_factory("Button 1", ["button"]) event = event_factory(device.device_id, capability="button", attribute="button", value="pushed") request = event_request_factory(events=[event]) config_entry.data[CONF_INSTALLED_APP_ID] = request.installed_app_id called = False def handler(evt): nonlocal called called = True assert evt.data == { "component_id": "main", "device_id": device.device_id, "location_id": event.location_id, "value": "pushed", "name": device.label, "data": None, } hass.bus.async_listen(EVENT_BUTTON, handler) broker = smartthings.DeviceBroker(hass, config_entry, Mock(), Mock(), [device], []) broker.connect() # pylint:disable=protected-access await broker._event_handler(request, None, None) await hass.async_block_till_done() assert called
async def test_event_handler_fires_button_events(hass, device_factory, event_factory, event_request_factory): """Test the event handler fires button events.""" device = device_factory('Button 1', ['button']) event = event_factory(device.device_id, capability='button', attribute='button', value='pushed') request = event_request_factory(events=[event]) called = False def handler(evt): nonlocal called called = True assert evt.data == { 'component_id': 'main', 'device_id': device.device_id, 'location_id': event.location_id, 'value': 'pushed', 'name': device.label } hass.bus.async_listen(EVENT_BUTTON, handler) broker = smartthings.DeviceBroker(hass, [device], request.installed_app_id) await broker.event_handler(request, None, None) await hass.async_block_till_done() assert called
async def test_event_handler_dispatches_updated_devices( hass, device_factory, event_request_factory): """Test the event handler dispatches updated devices.""" devices = [ device_factory('Bedroom 1 Switch', ['switch']), device_factory('Bathroom 1', ['switch']), device_factory('Sensor', ['motionSensor']), ] device_ids = [ devices[0].device_id, devices[1].device_id, devices[2].device_id ] request = event_request_factory(device_ids) called = False def signal(ids): nonlocal called called = True assert device_ids == ids async_dispatcher_connect(hass, SIGNAL_SMARTTHINGS_UPDATE, signal) broker = smartthings.DeviceBroker(hass, devices, request.installed_app_id) await broker.event_handler(request, None, None) await hass.async_block_till_done() assert called for device in devices: assert device.status.values['Updated'] == 'Value'
async def test_event_handler_fires_button_events( hass, config_entry, device_factory, event_factory, event_request_factory): """Test the event handler fires button events.""" device = device_factory('Button 1', ['button']) event = event_factory(device.device_id, capability='button', attribute='button', value='pushed') request = event_request_factory(events=[event]) config_entry.data[CONF_INSTALLED_APP_ID] = request.installed_app_id called = False def handler(evt): nonlocal called called = True assert evt.data == { 'component_id': 'main', 'device_id': device.device_id, 'location_id': event.location_id, 'value': 'pushed', 'name': device.label, 'data': None } hass.bus.async_listen(EVENT_BUTTON, handler) broker = smartthings.DeviceBroker( hass, config_entry, Mock(), Mock(), [device], []) broker.connect() # pylint:disable=protected-access await broker._event_handler(request, None, None) await hass.async_block_till_done() assert called
async def test_event_handler_dispatches_updated_devices( hass, config_entry, device_factory, event_request_factory, event_factory): """Test the event handler dispatches updated devices.""" devices = [ device_factory("Bedroom 1 Switch", ["switch"]), device_factory("Bathroom 1", ["switch"]), device_factory("Sensor", ["motionSensor"]), device_factory("Lock", ["lock"]), ] device_ids = [ devices[0].device_id, devices[1].device_id, devices[2].device_id, devices[3].device_id, ] event = event_factory( devices[3].device_id, capability="lock", attribute="lock", value="locked", data={"codeId": "1"}, ) request = event_request_factory(device_ids=device_ids, events=[event]) config_entry.data = { **config_entry.data, CONF_INSTALLED_APP_ID: request.installed_app_id, } called = False def signal(ids): nonlocal called called = True assert device_ids == ids async_dispatcher_connect(hass, SIGNAL_SMARTTHINGS_UPDATE, signal) broker = smartthings.DeviceBroker(hass, config_entry, Mock(), Mock(), devices, []) broker.connect() # pylint:disable=protected-access await broker._event_handler(request, None, None) await hass.async_block_till_done() assert called for device in devices: assert device.status.values["Updated"] == "Value" assert devices[3].status.attributes["lock"].value == "locked" assert devices[3].status.attributes["lock"].data == {"codeId": "1"}
async def test_event_handler_ignores_other_installed_app( hass, device_factory, event_request_factory): """Test the event handler dispatches updated devices.""" device = device_factory('Bedroom 1 Switch', ['switch']) request = event_request_factory([device.device_id]) called = False def signal(ids): nonlocal called called = True async_dispatcher_connect(hass, SIGNAL_SMARTTHINGS_UPDATE, signal) broker = smartthings.DeviceBroker(hass, [device], str(uuid4())) await broker.event_handler(request, None, None) await hass.async_block_till_done() assert not called
async def test_unload_entry(hass, config_entry): """Test entries are unloaded correctly.""" connect_disconnect = Mock() smart_app = Mock() smart_app.connect_event.return_value = connect_disconnect broker = smartthings.DeviceBroker( hass, config_entry, Mock(), smart_app, [], []) broker.connect() hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id] = broker with patch.object(hass.config_entries, 'async_forward_entry_unload', return_value=True) as forward_mock: assert await smartthings.async_unload_entry(hass, config_entry) assert connect_disconnect.call_count == 1 assert config_entry.entry_id not in hass.data[DOMAIN][DATA_BROKERS] # Assert platforms unloaded await hass.async_block_till_done() assert forward_mock.call_count == len(SUPPORTED_PLATFORMS)
async def test_event_handler_dispatches_updated_devices( hass, config_entry, device_factory, event_request_factory, event_factory): """Test the event handler dispatches updated devices.""" devices = [ device_factory('Bedroom 1 Switch', ['switch']), device_factory('Bathroom 1', ['switch']), device_factory('Sensor', ['motionSensor']), device_factory('Lock', ['lock']) ] device_ids = [ devices[0].device_id, devices[1].device_id, devices[2].device_id, devices[3].device_id ] event = event_factory(devices[3].device_id, capability='lock', attribute='lock', value='locked', data={'codeId': '1'}) request = event_request_factory(device_ids=device_ids, events=[event]) config_entry.data[CONF_INSTALLED_APP_ID] = request.installed_app_id called = False def signal(ids): nonlocal called called = True assert device_ids == ids async_dispatcher_connect(hass, SIGNAL_SMARTTHINGS_UPDATE, signal) broker = smartthings.DeviceBroker(hass, config_entry, Mock(), Mock(), devices, []) broker.connect() # pylint:disable=protected-access await broker._event_handler(request, None, None) await hass.async_block_till_done() assert called for device in devices: assert device.status.values['Updated'] == 'Value' assert devices[3].status.attributes['lock'].value == 'locked' assert devices[3].status.attributes['lock'].data == {'codeId': '1'}
async def test_event_handler_ignores_other_installed_app( hass, config_entry, device_factory, event_request_factory): """Test the event handler dispatches updated devices.""" device = device_factory("Bedroom 1 Switch", ["switch"]) request = event_request_factory([device.device_id]) called = False def signal(ids): nonlocal called called = True async_dispatcher_connect(hass, SIGNAL_SMARTTHINGS_UPDATE, signal) broker = smartthings.DeviceBroker(hass, config_entry, Mock(), Mock(), [device], []) broker.connect() # pylint:disable=protected-access await broker._event_handler(request, None, None) await hass.async_block_till_done() assert not called
async def test_broker_regenerates_token(hass, config_entry): """Test the device broker regenerates the refresh token.""" token = Mock(OAuthToken) token.refresh_token = str(uuid4()) stored_action = None def async_track_time_interval(hass, action, interval): nonlocal stored_action stored_action = action with patch( "homeassistant.components.smartthings" ".async_track_time_interval", new=async_track_time_interval, ): broker = smartthings.DeviceBroker(hass, config_entry, token, Mock(), [], []) broker.connect() assert stored_action await stored_action(None) # pylint:disable=not-callable assert token.refresh.call_count == 1 assert config_entry.data[CONF_REFRESH_TOKEN] == token.refresh_token