Add Linear Garage Door integration (#91436)

* Add Linear Garage Door integration

* Add Linear Garage Door integration

* Remove light platform

* Add tests for diagnostics

* Changes suggested by Lash

* Minor refactoring

* Various improvements

* Catch up to dev, various fixes

* Fix DeviceInfo import

* Use the HA dt_util

* Update tests/components/linear_garage_door/test_cover.py

* Apply suggestions from code review

---------

Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
IceBotYT
2023-11-22 09:35:31 +01:00
committed by GitHub
co-authored by Robert Resch Erik Montnemery
parent 6c6e85f996
commit cbb5d7ea39
22 changed files with 1134 additions and 0 deletions
@@ -0,0 +1 @@
"""Tests for the Linear Garage Door integration."""
@@ -0,0 +1,161 @@
"""Test the Linear Garage Door config flow."""
from unittest.mock import patch
from linear_garage_door.errors import InvalidLoginError
from homeassistant import config_entries
from homeassistant.components.linear_garage_door.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from .util import async_init_integration
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
with patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.get_sites",
return_value=[{"id": "test-site-id", "name": "test-site-name"}],
), patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.close",
return_value=None,
), patch(
"uuid.uuid4", return_value="test-uuid"
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"email": "test-email",
"password": "test-password",
},
)
await hass.async_block_till_done()
with patch(
"homeassistant.components.linear_garage_door.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"site": "test-site-id"}
)
await hass.async_block_till_done()
assert result3["type"] == FlowResultType.CREATE_ENTRY
assert result3["title"] == "test-site-name"
assert result3["data"] == {
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_reauth(hass: HomeAssistant) -> None:
"""Test reauthentication."""
entry = await async_init_integration(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_REAUTH,
"entry_id": entry.entry_id,
"title_placeholders": {"name": entry.title},
"unique_id": entry.unique_id,
},
data=entry.data,
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
with patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.get_sites",
return_value=[{"id": "test-site-id", "name": "test-site-name"}],
), patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.close",
return_value=None,
), patch(
"uuid.uuid4", return_value="test-uuid"
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"email": "new-email",
"password": "new-password",
},
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.ABORT
assert result2["reason"] == "reauth_successful"
entries = hass.config_entries.async_entries()
assert len(entries) == 1
assert entries[0].data == {
"email": "new-email",
"password": "new-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
}
async def test_form_invalid_login(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.login",
side_effect=InvalidLoginError,
), patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.close",
return_value=None,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"email": "test-email",
"password": "test-password",
},
)
assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_exception(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
)
with patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.login",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"email": "test-email",
"password": "test-password",
},
)
assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
@@ -0,0 +1,99 @@
"""Test data update coordinator for Linear Garage Door."""
from unittest.mock import patch
from linear_garage_door.errors import InvalidLoginError, ResponseError
from homeassistant.components.linear_garage_door.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_invalid_password(
hass: HomeAssistant,
) -> None:
"""Test invalid password."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.login",
side_effect=InvalidLoginError(
"Login provided is invalid, please check the email and password"
),
):
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(DOMAIN)
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.SETUP_ERROR
flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN)
assert flows
assert len(flows) == 1
assert flows[0]["context"]["source"] == "reauth"
async def test_response_error(hass: HomeAssistant) -> None:
"""Test response error."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.login",
side_effect=ResponseError,
):
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(DOMAIN)
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.SETUP_RETRY
async def test_invalid_login(
hass: HomeAssistant,
) -> None:
"""Test invalid login."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.login",
side_effect=InvalidLoginError("Some other error"),
):
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(DOMAIN)
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.SETUP_RETRY
@@ -0,0 +1,187 @@
"""Test Linear Garage Door cover."""
from datetime import timedelta
from unittest.mock import patch
from homeassistant.components.cover import (
DOMAIN as COVER_DOMAIN,
SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER,
STATE_CLOSED,
STATE_CLOSING,
STATE_OPEN,
STATE_OPENING,
)
from homeassistant.components.linear_garage_door.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util
from .util import async_init_integration
from tests.common import async_fire_time_changed
async def test_data(hass: HomeAssistant) -> None:
"""Test that data gets parsed and returned appropriately."""
await async_init_integration(hass)
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN)
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
assert hass.states.get("cover.test_garage_1").state == STATE_OPEN
assert hass.states.get("cover.test_garage_2").state == STATE_CLOSED
assert hass.states.get("cover.test_garage_3").state == STATE_OPENING
assert hass.states.get("cover.test_garage_4").state == STATE_CLOSING
async def test_open_cover(hass: HomeAssistant) -> None:
"""Test that opening the cover works as intended."""
await async_init_integration(hass)
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.operate_device"
) as operate_device:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_1"},
blocking=True,
)
assert operate_device.call_count == 0
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.operate_device",
return_value=None,
) as operate_device, patch(
"homeassistant.components.linear_garage_door.cover.Linear.close",
return_value=True,
):
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_2"},
blocking=True,
)
assert operate_device.call_count == 1
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.get_devices",
return_value=[
{"id": "test1", "name": "Test Garage 1", "subdevices": ["GDO", "Light"]},
{"id": "test2", "name": "Test Garage 2", "subdevices": ["GDO", "Light"]},
],
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.get_device_state",
side_effect=lambda id: {
"test1": {
"GDO": {"Open_B": "true", "Open_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
"test2": {
"GDO": {"Open_B": "false", "Opening_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test3": {
"GDO": {"Open_B": "false", "Opening_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test4": {
"GDO": {"Open_B": "true", "Opening_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
}[id],
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.close",
return_value=True,
):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=60))
await hass.async_block_till_done()
assert hass.states.get("cover.test_garage_2").state == STATE_OPENING
async def test_close_cover(hass: HomeAssistant) -> None:
"""Test that closing the cover works as intended."""
await async_init_integration(hass)
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.operate_device"
) as operate_device:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_2"},
blocking=True,
)
assert operate_device.call_count == 0
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.operate_device",
return_value=None,
) as operate_device, patch(
"homeassistant.components.linear_garage_door.cover.Linear.close",
return_value=True,
):
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_1"},
blocking=True,
)
assert operate_device.call_count == 1
with patch(
"homeassistant.components.linear_garage_door.cover.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.get_devices",
return_value=[
{"id": "test1", "name": "Test Garage 1", "subdevices": ["GDO", "Light"]},
{"id": "test2", "name": "Test Garage 2", "subdevices": ["GDO", "Light"]},
],
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.get_device_state",
side_effect=lambda id: {
"test1": {
"GDO": {"Open_B": "true", "Opening_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
"test2": {
"GDO": {"Open_B": "false", "Open_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test3": {
"GDO": {"Open_B": "false", "Opening_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test4": {
"GDO": {"Open_B": "true", "Opening_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
}[id],
), patch(
"homeassistant.components.linear_garage_door.cover.Linear.close",
return_value=True,
):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=60))
await hass.async_block_till_done()
assert hass.states.get("cover.test_garage_1").state == STATE_CLOSING
@@ -0,0 +1,53 @@
"""Test diagnostics of Linear Garage Door."""
from homeassistant.core import HomeAssistant
from .util import async_init_integration
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
async def test_entry_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test config entry diagnostics."""
entry = await async_init_integration(hass)
result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert result["entry"]["data"] == {
"email": "**REDACTED**",
"password": "**REDACTED**",
"site_id": "test-site-id",
"device_id": "test-uuid",
}
assert result["coordinator_data"] == {
"test1": {
"name": "Test Garage 1",
"subdevices": {
"GDO": {"Open_B": "true", "Open_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
},
"test2": {
"name": "Test Garage 2",
"subdevices": {
"GDO": {"Open_B": "false", "Open_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
},
"test3": {
"name": "Test Garage 3",
"subdevices": {
"GDO": {"Open_B": "false", "Opening_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
},
"test4": {
"name": "Test Garage 4",
"subdevices": {
"GDO": {"Open_B": "true", "Opening_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
},
}
@@ -0,0 +1,59 @@
"""Test Linear Garage Door init."""
from unittest.mock import patch
from homeassistant.components.linear_garage_door.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test the unload entry."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.get_devices",
return_value=[
{"id": "test", "name": "Test Garage", "subdevices": ["GDO", "Light"]}
],
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.get_device_state",
return_value={
"GDO": {"Open_B": "true", "Open_P": "100"},
"Light": {"On_B": "true", "On_P": "10"},
},
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.close",
return_value=True,
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN)
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.close",
return_value=True,
):
await hass.config_entries.async_unload(entries[0].entry_id)
await hass.async_block_till_done()
assert entries[0].state == ConfigEntryState.NOT_LOADED
@@ -0,0 +1,62 @@
"""Utilities for Linear Garage Door testing."""
from unittest.mock import patch
from homeassistant.components.linear_garage_door.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def async_init_integration(hass: HomeAssistant) -> MockConfigEntry:
"""Initialize mock integration."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"email": "test-email",
"password": "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.login",
return_value=True,
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.get_devices",
return_value=[
{"id": "test1", "name": "Test Garage 1", "subdevices": ["GDO", "Light"]},
{"id": "test2", "name": "Test Garage 2", "subdevices": ["GDO", "Light"]},
{"id": "test3", "name": "Test Garage 3", "subdevices": ["GDO", "Light"]},
{"id": "test4", "name": "Test Garage 4", "subdevices": ["GDO", "Light"]},
],
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.get_device_state",
side_effect=lambda id: {
"test1": {
"GDO": {"Open_B": "true", "Open_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
"test2": {
"GDO": {"Open_B": "false", "Open_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test3": {
"GDO": {"Open_B": "false", "Opening_P": "0"},
"Light": {"On_B": "false", "On_P": "0"},
},
"test4": {
"GDO": {"Open_B": "true", "Opening_P": "100"},
"Light": {"On_B": "true", "On_P": "100"},
},
}[id],
), patch(
"homeassistant.components.linear_garage_door.coordinator.Linear.close",
return_value=True,
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry