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 <erik@montnemery.com>

* 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 <erik@montnemery.com>
This commit is contained in:
Tobias Sauerwein 2021-03-19 12:19:27 +01:00 committed by GitHub
parent 7858b59944
commit aaafe399a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 1 deletions

View File

@ -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

View File

@ -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."""

View File

@ -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

View File

@ -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",
)

4
tests/fixtures/netatmo/ping.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"local_url": "http://192.168.0.123/678460a0d47e5618699fb31169e2b47d",
"product_name": "Welcome Netatmo"
}