Add Abode tests (#32562)
* Add tests for several devices * Update coveragerc * Code review changes and minor clean up * More code review changes * Update manifest and minor test updates * Add test for locks and covers * Add tests for switch on and off * Add more complete test for alarms * Fix for camera test * Patch abodepy.mode for tests * Add test for unknown alarm state and minor cleanup * Update to make tests more robust * More specific tests * Update quality scale to silver * Remove integration quality scale
This commit is contained in:
1 parent
d62bb9ed47
commit
d36259f067
18 files changed
+1904
-9
No files matched your search
@@ -0,0 +1,25 @@
|
||||
"""Common methods used across tests for Abode."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import DOMAIN as ABODE_DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_platform(hass, platform):
|
||||
"""Set up the Abode platform."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=ABODE_DOMAIN,
|
||||
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.abode.ABODE_PLATFORMS", [platform]), patch(
|
||||
"abodepy.event_controller.sio"
|
||||
), patch("abodepy.utils.save_cache"):
|
||||
assert await async_setup_component(hass, ABODE_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_entry
|
||||
@@ -0,0 +1,22 @@
|
||||
"""Configuration for Abode tests."""
|
||||
import abodepy.helpers.constants as CONST
|
||||
import pytest
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def requests_mock_fixture(requests_mock):
|
||||
"""Fixture to provide a requests mocker."""
|
||||
# Mocks the login response for abodepy.
|
||||
requests_mock.post(CONST.LOGIN_URL, text=load_fixture("abode_login.json"))
|
||||
# Mocks the oauth claims response for abodepy.
|
||||
requests_mock.get(
|
||||
CONST.OAUTH_TOKEN_URL, text=load_fixture("abode_oauth_claims.json")
|
||||
)
|
||||
# Mocks the panel response for abodepy.
|
||||
requests_mock.get(CONST.PANEL_URL, text=load_fixture("abode_panel.json"))
|
||||
# Mocks the automations response for abodepy.
|
||||
requests_mock.get(CONST.AUTOMATION_URL, text=load_fixture("abode_automation.json"))
|
||||
# Mocks the devices response for abodepy.
|
||||
requests_mock.get(CONST.DEVICES_URL, text=load_fixture("abode_devices.json"))
|
||||
@@ -0,0 +1,140 @@
|
||||
"""Tests for the Abode alarm control panel device."""
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
import abodepy.helpers.constants as CONST
|
||||
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
SERVICE_ALARM_ARM_AWAY,
|
||||
SERVICE_ALARM_ARM_HOME,
|
||||
SERVICE_ALARM_DISARM,
|
||||
STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_DISARMED,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
DEVICE_ID = "alarm_control_panel.abode_alarm"
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(DEVICE_ID)
|
||||
# Abode alarm device unique_id is the MAC address
|
||||
assert entry.unique_id == "001122334455"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the alarm control panel attributes are correct."""
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_ALARM_DISARMED
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "area_1"
|
||||
assert not state.attributes.get("battery_backup")
|
||||
assert not state.attributes.get("cellular_backup")
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Abode Alarm"
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 3
|
||||
|
||||
|
||||
async def test_set_alarm_away(hass):
|
||||
"""Test the alarm control panel can be set to away."""
|
||||
with patch("abodepy.AbodeEventController.add_device_callback") as mock_callback:
|
||||
with patch("abodepy.ALARM.AbodeAlarm.set_away") as mock_set_away:
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN,
|
||||
SERVICE_ALARM_ARM_AWAY,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_away.assert_called_once()
|
||||
|
||||
with patch(
|
||||
"abodepy.ALARM.AbodeAlarm.mode", new_callable=PropertyMock,
|
||||
) as mock_mode:
|
||||
mock_mode.return_value = CONST.MODE_AWAY
|
||||
|
||||
update_callback = mock_callback.call_args[0][1]
|
||||
await hass.async_add_executor_job(update_callback, "area_1")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||
|
||||
|
||||
async def test_set_alarm_home(hass):
|
||||
"""Test the alarm control panel can be set to home."""
|
||||
with patch("abodepy.AbodeEventController.add_device_callback") as mock_callback:
|
||||
with patch("abodepy.ALARM.AbodeAlarm.set_home") as mock_set_home:
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN,
|
||||
SERVICE_ALARM_ARM_HOME,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_home.assert_called_once()
|
||||
|
||||
with patch(
|
||||
"abodepy.ALARM.AbodeAlarm.mode", new_callable=PropertyMock
|
||||
) as mock_mode:
|
||||
mock_mode.return_value = CONST.MODE_HOME
|
||||
|
||||
update_callback = mock_callback.call_args[0][1]
|
||||
await hass.async_add_executor_job(update_callback, "area_1")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_ALARM_ARMED_HOME
|
||||
|
||||
|
||||
async def test_set_alarm_standby(hass):
|
||||
"""Test the alarm control panel can be set to standby."""
|
||||
with patch("abodepy.AbodeEventController.add_device_callback") as mock_callback:
|
||||
with patch("abodepy.ALARM.AbodeAlarm.set_standby") as mock_set_standby:
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN,
|
||||
SERVICE_ALARM_DISARM,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_standby.assert_called_once()
|
||||
|
||||
with patch(
|
||||
"abodepy.ALARM.AbodeAlarm.mode", new_callable=PropertyMock
|
||||
) as mock_mode:
|
||||
mock_mode.return_value = CONST.MODE_STANDBY
|
||||
|
||||
update_callback = mock_callback.call_args[0][1]
|
||||
await hass.async_add_executor_job(update_callback, "area_1")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_ALARM_DISARMED
|
||||
|
||||
|
||||
async def test_state_unknown(hass):
|
||||
"""Test an unknown alarm control panel state."""
|
||||
with patch("abodepy.ALARM.AbodeAlarm.mode", new_callable=PropertyMock) as mock_mode:
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_mode.return_value = None
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == "unknown"
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Tests for the Abode binary sensor device."""
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.abode.const import ATTRIBUTION
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_DOOR,
|
||||
DOMAIN as BINARY_SENSOR_DOMAIN,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ATTRIBUTION,
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
STATE_OFF,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, BINARY_SENSOR_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get("binary_sensor.front_door")
|
||||
assert entry.unique_id == "2834013428b6035fba7d4054aa7b25a3"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the binary sensor attributes are correct."""
|
||||
await setup_platform(hass, BINARY_SENSOR_DOMAIN)
|
||||
|
||||
state = hass.states.get("binary_sensor.front_door")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "RF:01430030"
|
||||
assert not state.attributes.get("battery_low")
|
||||
assert not state.attributes.get("no_response")
|
||||
assert state.attributes.get("device_type") == "Door Contact"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Front Door"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_DOOR
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Tests for the Abode camera device."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode.const import DOMAIN as ABODE_DOMAIN
|
||||
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_IDLE
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, CAMERA_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get("camera.test_cam")
|
||||
assert entry.unique_id == "d0a3a1c316891ceb00c20118aae2a133"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the camera attributes are correct."""
|
||||
await setup_platform(hass, CAMERA_DOMAIN)
|
||||
|
||||
state = hass.states.get("camera.test_cam")
|
||||
assert state.state == STATE_IDLE
|
||||
|
||||
|
||||
async def test_capture_image(hass):
|
||||
"""Test the camera capture image service."""
|
||||
await setup_platform(hass, CAMERA_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeCamera.capture") as mock_capture:
|
||||
await hass.services.async_call(
|
||||
ABODE_DOMAIN,
|
||||
"capture_image",
|
||||
{ATTR_ENTITY_ID: "camera.test_cam"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_capture.assert_called_once()
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Tests for the Abode cover device."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
SERVICE_CLOSE_COVER,
|
||||
SERVICE_OPEN_COVER,
|
||||
STATE_CLOSED,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
DEVICE_ID = "cover.garage_door"
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, COVER_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(DEVICE_ID)
|
||||
assert entry.unique_id == "61cbz3b542d2o33ed2fz02721bda3324"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the cover attributes are correct."""
|
||||
await setup_platform(hass, COVER_DOMAIN)
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_CLOSED
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "ZW:00000007"
|
||||
assert not state.attributes.get("battery_low")
|
||||
assert not state.attributes.get("no_response")
|
||||
assert state.attributes.get("device_type") == "Secure Barrier"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Garage Door"
|
||||
|
||||
|
||||
async def test_open(hass):
|
||||
"""Test the cover can be opened."""
|
||||
await setup_platform(hass, COVER_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeCover.open_cover") as mock_open:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_open.assert_called_once()
|
||||
|
||||
|
||||
async def test_close(hass):
|
||||
"""Test the cover can be closed."""
|
||||
await setup_platform(hass, COVER_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeCover.close_cover") as mock_close:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_close.assert_called_once()
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Tests for the Abode module."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import (
|
||||
DOMAIN as ABODE_DOMAIN,
|
||||
SERVICE_CAPTURE_IMAGE,
|
||||
SERVICE_SETTINGS,
|
||||
SERVICE_TRIGGER_AUTOMATION,
|
||||
)
|
||||
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
|
||||
async def test_change_settings(hass):
|
||||
"""Test change_setting service."""
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
|
||||
with patch("abodepy.Abode.set_setting") as mock_set_setting:
|
||||
await hass.services.async_call(
|
||||
ABODE_DOMAIN,
|
||||
SERVICE_SETTINGS,
|
||||
{"setting": "confirm_snd", "value": "loud"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_setting.assert_called_once()
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test unloading the Abode entry."""
|
||||
mock_entry = await setup_platform(hass, ALARM_DOMAIN)
|
||||
|
||||
with patch("abodepy.Abode.logout") as mock_logout, patch(
|
||||
"abodepy.event_controller.AbodeEventController.stop"
|
||||
) as mock_events_stop:
|
||||
assert await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
mock_logout.assert_called_once()
|
||||
mock_events_stop.assert_called_once()
|
||||
|
||||
assert not hass.services.has_service(ABODE_DOMAIN, SERVICE_SETTINGS)
|
||||
assert not hass.services.has_service(ABODE_DOMAIN, SERVICE_CAPTURE_IMAGE)
|
||||
assert not hass.services.has_service(ABODE_DOMAIN, SERVICE_TRIGGER_AUTOMATION)
|
||||
@@ -0,0 +1,119 @@
|
||||
"""Tests for the Abode light device."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_TEMP,
|
||||
ATTR_RGB_COLOR,
|
||||
DOMAIN as LIGHT_DOMAIN,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
DEVICE_ID = "light.living_room_lamp"
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(DEVICE_ID)
|
||||
assert entry.unique_id == "741385f4388b2637df4c6b398fe50581"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the light attributes are correct."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_BRIGHTNESS) == 204
|
||||
assert state.attributes.get(ATTR_RGB_COLOR) == (0, 63, 255)
|
||||
assert state.attributes.get(ATTR_COLOR_TEMP) == 280
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "ZB:db5b1a"
|
||||
assert not state.attributes.get("battery_low")
|
||||
assert not state.attributes.get("no_response")
|
||||
assert state.attributes.get("device_type") == "RGB Dimmer"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Living Room Lamp"
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 19
|
||||
|
||||
|
||||
async def test_switch_off(hass):
|
||||
"""Test the light can be turned off."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLight.switch_off") as mock_switch_off:
|
||||
assert await hass.services.async_call(
|
||||
LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_switch_off.assert_called_once()
|
||||
|
||||
|
||||
async def test_switch_on(hass):
|
||||
"""Test the light can be turned on."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLight.switch_on") as mock_switch_on:
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_switch_on.assert_called_once()
|
||||
|
||||
|
||||
async def test_set_brightness(hass):
|
||||
"""Test the brightness can be set."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLight.set_level") as mock_set_level:
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID, "brightness": 100},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
# Brightness is converted in abode.light.AbodeLight.turn_on
|
||||
mock_set_level.assert_called_once_with(39)
|
||||
|
||||
|
||||
async def test_set_color(hass):
|
||||
"""Test the color can be set."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLight.set_color") as mock_set_color:
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID, "hs_color": [240, 100]},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_color.assert_called_once_with((240.0, 100.0))
|
||||
|
||||
|
||||
async def test_set_color_temp(hass):
|
||||
"""Test the color temp can be set."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLight.set_color_temp") as mock_set_color_temp:
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: DEVICE_ID, "color_temp": 309},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
# Color temp is converted in abode.light.AbodeLight.turn_on
|
||||
mock_set_color_temp.assert_called_once_with(3236)
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Tests for the Abode lock device."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
SERVICE_LOCK,
|
||||
SERVICE_UNLOCK,
|
||||
STATE_LOCKED,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
DEVICE_ID = "lock.test_lock"
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, LOCK_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(DEVICE_ID)
|
||||
assert entry.unique_id == "51cab3b545d2o34ed7fz02731bda5324"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the lock attributes are correct."""
|
||||
await setup_platform(hass, LOCK_DOMAIN)
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_LOCKED
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "ZW:00000004"
|
||||
assert not state.attributes.get("battery_low")
|
||||
assert not state.attributes.get("no_response")
|
||||
assert state.attributes.get("device_type") == "Door Lock"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Test Lock"
|
||||
|
||||
|
||||
async def test_lock(hass):
|
||||
"""Test the lock can be locked."""
|
||||
await setup_platform(hass, LOCK_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLock.lock") as mock_lock:
|
||||
await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_lock.assert_called_once()
|
||||
|
||||
|
||||
async def test_unlock(hass):
|
||||
"""Test the lock can be unlocked."""
|
||||
await setup_platform(hass, LOCK_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeLock.unlock") as mock_unlock:
|
||||
await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_unlock.assert_called_once()
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Tests for the Abode sensor device."""
|
||||
from homeassistant.components.abode import ATTR_DEVICE_ID
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, SENSOR_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get("sensor.environment_sensor_humidity")
|
||||
assert entry.unique_id == "13545b21f4bdcd33d9abd461f8443e65-humidity"
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the sensor attributes are correct."""
|
||||
await setup_platform(hass, SENSOR_DOMAIN)
|
||||
|
||||
state = hass.states.get("sensor.environment_sensor_humidity")
|
||||
assert state.state == "32.0"
|
||||
assert state.attributes.get(ATTR_DEVICE_ID) == "RF:02148e70"
|
||||
assert not state.attributes.get("battery_low")
|
||||
assert not state.attributes.get("no_response")
|
||||
assert state.attributes.get("device_type") == "LM"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "%"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Environment Sensor Humidity"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_HUMIDITY
|
||||
|
||||
state = hass.states.get("sensor.environment_sensor_lux")
|
||||
assert state.state == "1.0"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "lux"
|
||||
|
||||
state = hass.states.get("sensor.environment_sensor_temperature")
|
||||
# Abodepy device JSON reports 19.5, but Home Assistant shows 19.4
|
||||
assert state.state == "19.4"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "°C"
|
||||
@@ -0,0 +1,125 @@
|
||||
"""Tests for the Abode switch device."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.abode import (
|
||||
DOMAIN as ABODE_DOMAIN,
|
||||
SERVICE_TRIGGER_AUTOMATION,
|
||||
)
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
|
||||
from .common import setup_platform
|
||||
|
||||
AUTOMATION_ID = "switch.test_automation"
|
||||
AUTOMATION_UID = "47fae27488f74f55b964a81a066c3a01"
|
||||
DEVICE_ID = "switch.test_switch"
|
||||
DEVICE_UID = "0012a4d3614cb7e2b8c9abea31d2fb2a"
|
||||
|
||||
|
||||
async def test_entity_registry(hass):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(AUTOMATION_ID)
|
||||
assert entry.unique_id == AUTOMATION_UID
|
||||
|
||||
entry = entity_registry.async_get(DEVICE_ID)
|
||||
assert entry.unique_id == DEVICE_UID
|
||||
|
||||
|
||||
async def test_attributes(hass):
|
||||
"""Test the switch attributes are correct."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
state = hass.states.get(DEVICE_ID)
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_switch_on(hass):
|
||||
"""Test the switch can be turned on."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeSwitch.switch_on") as mock_switch_on:
|
||||
assert await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_switch_on.assert_called_once()
|
||||
|
||||
|
||||
async def test_switch_off(hass):
|
||||
"""Test the switch can be turned off."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeSwitch.switch_off") as mock_switch_off:
|
||||
assert await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: DEVICE_ID}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_switch_off.assert_called_once()
|
||||
|
||||
|
||||
async def test_automation_attributes(hass):
|
||||
"""Test the automation attributes are correct."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
state = hass.states.get(AUTOMATION_ID)
|
||||
# State is set based on "enabled" key in automation JSON.
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_turn_automation_off(hass):
|
||||
"""Test the automation can be turned off."""
|
||||
with patch("abodepy.AbodeAutomation.enable") as mock_trigger:
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: AUTOMATION_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_trigger.assert_called_once_with(False)
|
||||
|
||||
|
||||
async def test_turn_automation_on(hass):
|
||||
"""Test the automation can be turned on."""
|
||||
with patch("abodepy.AbodeAutomation.enable") as mock_trigger:
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: AUTOMATION_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_trigger.assert_called_once_with(True)
|
||||
|
||||
|
||||
async def test_trigger_automation(hass, requests_mock):
|
||||
"""Test the trigger automation service."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN)
|
||||
|
||||
with patch("abodepy.AbodeAutomation.trigger") as mock:
|
||||
await hass.services.async_call(
|
||||
ABODE_DOMAIN,
|
||||
SERVICE_TRIGGER_AUTOMATION,
|
||||
{ATTR_ENTITY_ID: AUTOMATION_ID},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock.assert_called_once()
|
||||
Reference in new issue
Block a user