diff --git a/.coveragerc b/.coveragerc index 5778d541a68..ccbdaa9c6fa 100644 --- a/.coveragerc +++ b/.coveragerc @@ -580,13 +580,8 @@ omit = homeassistant/components/neato/vacuum.py homeassistant/components/nederlandse_spoorwegen/sensor.py homeassistant/components/nello/lock.py - homeassistant/components/nest/__init__.py homeassistant/components/nest/api.py - homeassistant/components/nest/binary_sensor.py - homeassistant/components/nest/camera.py - homeassistant/components/nest/climate.py homeassistant/components/nest/legacy/* - homeassistant/components/nest/sensor.py homeassistant/components/netatmo/__init__.py homeassistant/components/netatmo/api.py homeassistant/components/netatmo/camera.py diff --git a/tests/components/nest/test_events.py b/tests/components/nest/test_events.py index 7295d134087..692507d6ff9 100644 --- a/tests/components/nest/test_events.py +++ b/tests/components/nest/test_events.py @@ -253,3 +253,41 @@ async def test_unknown_event(hass): await hass.async_block_till_done() assert len(events) == 0 + + +async def test_unknown_device_id(hass): + """Test a pubsub message for an unknown event type.""" + events = async_capture_events(hass, NEST_EVENT) + subscriber = await async_setup_devices( + hass, + "sdm.devices.types.DOORBELL", + create_device_traits("sdm.devices.traits.DoorbellChime"), + ) + await subscriber.async_receive_event( + create_event("sdm.devices.events.DoorbellChime.Chime", "invalid-device-id") + ) + await hass.async_block_till_done() + + assert len(events) == 0 + + +async def test_event_message_without_device_event(hass): + """Test a pubsub message for an unknown event type.""" + events = async_capture_events(hass, NEST_EVENT) + subscriber = await async_setup_devices( + hass, + "sdm.devices.types.DOORBELL", + create_device_traits("sdm.devices.traits.DoorbellChime"), + ) + timestamp = utcnow() + event = EventMessage( + { + "eventId": "some-event-id", + "timestamp": timestamp.isoformat(timespec="seconds"), + }, + auth=None, + ) + await subscriber.async_receive_event(event) + await hass.async_block_till_done() + + assert len(events) == 0 diff --git a/tests/components/nest/test_init_sdm.py b/tests/components/nest/test_init_sdm.py index cb17f81d18a..e49f3b89f27 100644 --- a/tests/components/nest/test_init_sdm.py +++ b/tests/components/nest/test_init_sdm.py @@ -7,11 +7,12 @@ and failure modes. import logging -from google_nest_sdm.exceptions import GoogleNestException +from google_nest_sdm.exceptions import AuthException, GoogleNestException from homeassistant.components.nest import DOMAIN from homeassistant.config_entries import ( ENTRY_STATE_LOADED, + ENTRY_STATE_NOT_LOADED, ENTRY_STATE_SETUP_ERROR, ENTRY_STATE_SETUP_RETRY, ) @@ -42,7 +43,7 @@ async def async_setup_sdm(hass, config=CONFIG): with patch( "homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation" ): - await async_setup_component(hass, DOMAIN, config) + return await async_setup_component(hass, DOMAIN, config) async def test_setup_configuration_failure(hass, caplog): @@ -50,7 +51,8 @@ async def test_setup_configuration_failure(hass, caplog): config = CONFIG.copy() config[DOMAIN]["subscriber_id"] = "invalid-subscriber-format" - await async_setup_sdm(hass, config) + result = await async_setup_sdm(hass, config) + assert result entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 @@ -67,7 +69,8 @@ async def test_setup_susbcriber_failure(hass, caplog): "homeassistant.components.nest.GoogleNestSubscriber.start_async", side_effect=GoogleNestException(), ), caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): - await async_setup_sdm(hass) + result = await async_setup_sdm(hass) + assert result assert "Subscriber error:" in caplog.text entries = hass.config_entries.async_entries(DOMAIN) @@ -81,10 +84,54 @@ async def test_setup_device_manager_failure(hass, caplog): "homeassistant.components.nest.GoogleNestSubscriber.async_get_device_manager", side_effect=GoogleNestException(), ), caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): - await async_setup_sdm(hass) + result = await async_setup_sdm(hass) + assert result assert len(caplog.messages) == 1 assert "Device manager error:" in caplog.text entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert entries[0].state == ENTRY_STATE_SETUP_RETRY + + +async def test_subscriber_auth_failure(hass, caplog): + """Test configuration error.""" + with patch( + "homeassistant.components.nest.GoogleNestSubscriber.start_async", + side_effect=AuthException(), + ): + result = await async_setup_sdm(hass, CONFIG) + assert result + + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + assert entries[0].state == ENTRY_STATE_SETUP_ERROR + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + assert flows[0]["step_id"] == "reauth_confirm" + + +async def test_setup_missing_subscriber_id(hass, caplog): + """Test successful setup.""" + config = CONFIG + del config[DOMAIN]["subscriber_id"] + with caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): + result = await async_setup_sdm(hass, config) + assert not result + assert "Configuration option" in caplog.text + + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + assert entries[0].state == ENTRY_STATE_NOT_LOADED + + +async def test_empty_config(hass, caplog): + """Test successful setup.""" + with caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): + result = await async_setup_component(hass, DOMAIN, {}) + assert result + assert not caplog.records + + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 0