Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
IceBotYT committed May 25, 2023
1 parent ef33ab6 commit 7835ba4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 52 deletions.
11 changes: 2 additions & 9 deletions homeassistant/components/linear_garage_door/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import uuid

from linear_garage_door import Linear
from linear_garage_door.errors import InvalidDeviceIDError, InvalidLoginError
from linear_garage_door.errors import InvalidLoginError
import voluptuous as vol

from homeassistant import config_entries
Expand Down Expand Up @@ -37,7 +37,7 @@ async def validate_input(

hub = Linear()

device_id = data.get("device_id", str(uuid.uuid4()))
device_id = str(uuid.uuid4())

try:
await hub.login(
Expand All @@ -48,8 +48,6 @@ async def validate_input(
)
except InvalidLoginError as err:
raise InvalidAuth from err
except InvalidDeviceIDError as err:
raise InvalidDeviceID from err

sites = await hub.get_sites()

Expand Down Expand Up @@ -81,9 +79,6 @@ async def async_step_user(
"""Handle the initial step."""
data_schema = STEP_USER_DATA_SCHEMA

if self.show_advanced_options:
data_schema["device_id"] = str

data_schema = vol.Schema(data_schema)

if user_input is None:
Expand All @@ -95,8 +90,6 @@ async def async_step_user(
info = await validate_input(self.hass, user_input)
except InvalidAuth:
errors["base"] = "invalid_auth"
except InvalidDeviceID:
errors["base"] = "invalid_device_id"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/linear_garage_door/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
hass,
_LOGGER,
name="Linear Garage Door",
update_interval=timedelta(seconds=5),
update_interval=timedelta(seconds=60),
)

async def _async_update_data(self) -> dict[str, Any]:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/linear_garage_door/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def __init__(
self._subdevice = subdevice
self._attr_device_class = CoverDeviceClass.GARAGE
self._attr_unique_id = f"{device_id}-{subdevice}"
self._attr_name = subdevice
self._config_entry = config_entry

@property
Expand All @@ -87,7 +86,7 @@ def device_info(self) -> DeviceInfo:
)

@property
def is_closed(self) -> bool: # sourcery skip: remove-unnecessary-cast
def is_closed(self) -> bool:
"""Return if cover is closed."""
return bool(
self.coordinator.data[self._device_id]["subdevices"][self._subdevice][
Expand All @@ -97,7 +96,7 @@ def is_closed(self) -> bool: # sourcery skip: remove-unnecessary-cast
)

@property
def is_opened(self) -> bool: # sourcery skip: remove-unnecessary-cast
def is_opened(self) -> bool:
"""Return if cover is open."""
return bool(
self.coordinator.data[self._device_id]["subdevices"][self._subdevice][
Expand All @@ -107,7 +106,7 @@ def is_opened(self) -> bool: # sourcery skip: remove-unnecessary-cast
)

@property
def is_opening(self) -> bool: # sourcery skip: remove-unnecessary-cast
def is_opening(self) -> bool:
"""Return if cover is opening."""
return bool(
self.coordinator.data[self._device_id]["subdevices"][self._subdevice].get(
Expand All @@ -117,7 +116,7 @@ def is_opening(self) -> bool: # sourcery skip: remove-unnecessary-cast
)

@property
def is_closing(self) -> bool: # sourcery skip: remove-unnecessary-cast
def is_closing(self) -> bool:
"""Return if cover is closing."""
return bool(
self.coordinator.data[self._device_id]["subdevices"][self._subdevice].get(
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/linear_garage_door/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
"user": {
"data": {
"email": "[%key:common::config_flow::data::email%]",
"password": "[%key:common::config_flow::data::password%]",
"device_id": "Device ID (leave blank to autogenerate)"
"password": "[%key:common::config_flow::data::password%]"
}
}
},
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]",
"invalid_device_id": "Invalid device ID. Device ID must be a valid UUID."
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
Expand Down
26 changes: 1 addition & 25 deletions tests/components/linear_garage_door/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from unittest.mock import patch

from linear_garage_door.errors import InvalidDeviceIDError, InvalidLoginError
from linear_garage_door.errors import InvalidLoginError

from homeassistant import config_entries
from homeassistant.components.linear_garage_door.const import DOMAIN
Expand Down Expand Up @@ -135,30 +135,6 @@ async def test_form_invalid_login(hass: HomeAssistant) -> None:
assert result2["errors"] == {"base": "invalid_auth"}


async def test_form_invalid_device_id(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER, "show_advanced_options": True},
)

with patch(
"homeassistant.components.linear_garage_door.config_flow.Linear.login",
side_effect=InvalidDeviceIDError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"email": "test-email",
"password": "test-password",
"device_id": "invalid-device-id",
},
)

assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_device_id"}


async def test_form_exception(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
Expand Down
16 changes: 8 additions & 8 deletions tests/components/linear_garage_door/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async def test_data(hass: HomeAssistant) -> None:
assert entries
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
assert hass.states.get("cover.test_garage_1_gdo").state == STATE_OPEN
assert hass.states.get("cover.test_garage_2_gdo").state == STATE_CLOSED
assert hass.states.get("cover.test_garage_1").state == STATE_OPEN
assert hass.states.get("cover.test_garage_2").state == STATE_CLOSED


async def test_open_cover(hass: HomeAssistant) -> None:
Expand All @@ -47,7 +47,7 @@ async def test_open_cover(hass: HomeAssistant) -> None:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_1_gdo"},
{ATTR_ENTITY_ID: "cover.test_garage_1"},
blocking=True,
)

Expand All @@ -66,7 +66,7 @@ async def test_open_cover(hass: HomeAssistant) -> None:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_2_gdo"},
{ATTR_ENTITY_ID: "cover.test_garage_2"},
blocking=True,
)

Expand Down Expand Up @@ -99,7 +99,7 @@ async def test_open_cover(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()

assert hass.states.get("cover.test_garage_2_gdo").state == STATE_OPENING
assert hass.states.get("cover.test_garage_2").state == STATE_OPENING


async def test_close_cover(hass: HomeAssistant) -> None:
Expand All @@ -113,7 +113,7 @@ async def test_close_cover(hass: HomeAssistant) -> None:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_2_gdo"},
{ATTR_ENTITY_ID: "cover.test_garage_2"},
blocking=True,
)

Expand All @@ -132,7 +132,7 @@ async def test_close_cover(hass: HomeAssistant) -> None:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: "cover.test_garage_1_gdo"},
{ATTR_ENTITY_ID: "cover.test_garage_1"},
blocking=True,
)

Expand Down Expand Up @@ -165,4 +165,4 @@ async def test_close_cover(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()

assert hass.states.get("cover.test_garage_1_gdo").state == STATE_CLOSING
assert hass.states.get("cover.test_garage_1").state == STATE_CLOSING

0 comments on commit 7835ba4

Please sign in to comment.