From aaafe399a1804410d1d5a227950cd19cba3aa8cc Mon Sep 17 00:00:00 2001 From: Tobias Sauerwein Date: Fri, 19 Mar 2021 12:19:27 +0100 Subject: [PATCH] Add tests for Netatmo light (#46381) * Add tests for Netatmo light * Improve docstring * Register the camera data class for the light platform * Remove freezegun dependency * Update tests * Update tests/components/netatmo/test_light.py Co-authored-by: Erik Montnemery * Deduplicate webhook test data * Mock pytest to verify it is called * Don't test internals * Rename * Assert light still on with erroneous event data Co-authored-by: Erik Montnemery --- .coveragerc | 1 - homeassistant/components/netatmo/light.py | 6 ++ tests/components/netatmo/conftest.py | 6 ++ tests/components/netatmo/test_light.py | 79 +++++++++++++++++++++++ tests/fixtures/netatmo/ping.json | 4 ++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/components/netatmo/test_light.py create mode 100644 tests/fixtures/netatmo/ping.json diff --git a/.coveragerc b/.coveragerc index 92d55e83527..71dbda8ce40 100644 --- a/.coveragerc +++ b/.coveragerc @@ -647,7 +647,6 @@ omit = homeassistant/components/netatmo/camera.py homeassistant/components/netatmo/data_handler.py homeassistant/components/netatmo/helper.py - homeassistant/components/netatmo/light.py homeassistant/components/netatmo/netatmo_entity_base.py homeassistant/components/netatmo/sensor.py homeassistant/components/netatmo/webhook.py diff --git a/homeassistant/components/netatmo/light.py b/homeassistant/components/netatmo/light.py index eed12f048c8..08744c462e8 100644 --- a/homeassistant/components/netatmo/light.py +++ b/homeassistant/components/netatmo/light.py @@ -31,6 +31,10 @@ async def async_setup_entry(hass, entry, async_add_entities): data_handler = hass.data[DOMAIN][entry.entry_id][DATA_HANDLER] + await data_handler.register_data_class( + CAMERA_DATA_CLASS_NAME, CAMERA_DATA_CLASS_NAME, None + ) + if CAMERA_DATA_CLASS_NAME not in data_handler.data: raise PlatformNotReady @@ -64,6 +68,8 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(await get_entities(), True) + await data_handler.unregister_data_class(CAMERA_DATA_CLASS_NAME, None) + class NetatmoLight(NetatmoBase, LightEntity): """Representation of a Netatmo Presence camera light.""" diff --git a/tests/components/netatmo/conftest.py b/tests/components/netatmo/conftest.py index b18b70f323e..4e59eaacb82 100644 --- a/tests/components/netatmo/conftest.py +++ b/tests/components/netatmo/conftest.py @@ -81,6 +81,8 @@ async def mock_sensor_entry_fixture(hass, config_entry): """Mock setup of sensor platform.""" with selected_platforms(["sensor"]): await hass.config_entries.async_setup(config_entry.entry_id) + + await hass.async_block_till_done() return config_entry @@ -89,6 +91,8 @@ async def mock_camera_entry_fixture(hass, config_entry): """Mock setup of camera platform.""" with selected_platforms(["camera"]): await hass.config_entries.async_setup(config_entry.entry_id) + + await hass.async_block_till_done() return config_entry @@ -97,6 +101,8 @@ async def mock_light_entry_fixture(hass, config_entry): """Mock setup of light platform.""" with selected_platforms(["light"]): await hass.config_entries.async_setup(config_entry.entry_id) + + await hass.async_block_till_done() return config_entry diff --git a/tests/components/netatmo/test_light.py b/tests/components/netatmo/test_light.py new file mode 100644 index 00000000000..8f2f371a15e --- /dev/null +++ b/tests/components/netatmo/test_light.py @@ -0,0 +1,79 @@ +"""The tests for Netatmo light.""" +from unittest.mock import patch + +from homeassistant.components.light import ( + DOMAIN as LIGHT_DOMAIN, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.const import ATTR_ENTITY_ID, CONF_WEBHOOK_ID + +from .common import simulate_webhook + + +async def test_light_setup_and_services(hass, light_entry): + """Test setup and services.""" + webhook_id = light_entry.data[CONF_WEBHOOK_ID] + + # Fake webhook activation + webhook_data = { + "push_type": "webhook_activation", + } + await simulate_webhook(hass, webhook_id, webhook_data) + await hass.async_block_till_done() + + light_entity = "light.netatmo_garden" + assert hass.states.get(light_entity).state == "unavailable" + + # Trigger light mode change + response = { + "event_type": "light_mode", + "device_id": "12:34:56:00:a5:a4", + "camera_id": "12:34:56:00:a5:a4", + "event_id": "601dce1560abca1ebad9b723", + "push_type": "NOC-light_mode", + "sub_type": "on", + } + await simulate_webhook(hass, webhook_id, response) + + assert hass.states.get(light_entity).state == "on" + + # Trigger light mode change with erroneous webhook data + response = { + "user_id": "91763b24c43d3e344f424e8d", + "event_type": "light_mode", + "device_id": "12:34:56:00:a5:a4", + } + await simulate_webhook(hass, webhook_id, response) + + assert hass.states.get(light_entity).state == "on" + + # Test turning light off + with patch("pyatmo.camera.CameraData.set_state") as mock_set_state: + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: light_entity}, + blocking=True, + ) + await hass.async_block_till_done() + mock_set_state.assert_called_once_with( + home_id="91763b24c43d3e344f424e8b", + camera_id="12:34:56:00:a5:a4", + floodlight="auto", + ) + + # Test turning light on + with patch("pyatmo.camera.CameraData.set_state") as mock_set_state: + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: light_entity}, + blocking=True, + ) + await hass.async_block_till_done() + mock_set_state.assert_called_once_with( + home_id="91763b24c43d3e344f424e8b", + camera_id="12:34:56:00:a5:a4", + floodlight="on", + ) diff --git a/tests/fixtures/netatmo/ping.json b/tests/fixtures/netatmo/ping.json new file mode 100644 index 00000000000..784975de5b0 --- /dev/null +++ b/tests/fixtures/netatmo/ping.json @@ -0,0 +1,4 @@ +{ + "local_url": "http://192.168.0.123/678460a0d47e5618699fb31169e2b47d", + "product_name": "Welcome Netatmo" +} \ No newline at end of file