Skip to content

Commit

Permalink
Address Blink late review (home-assistant#102106)
Browse files Browse the repository at this point in the history
* Address late review topics

* Missing await, optimize config_flow call

* Address proper mock for blink

* Address typing
  • Loading branch information
mkmer authored Oct 16, 2023
1 parent e151358 commit 9444e1e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
7 changes: 4 additions & 3 deletions homeassistant/components/blink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

try:
await blink.start()
if blink.auth.check_key_required():
_LOGGER.debug("Attempting a reauth flow")
raise ConfigEntryAuthFailed("Need 2FA for Blink")
except (ClientError, asyncio.TimeoutError) as ex:
raise ConfigEntryNotReady("Can not connect to host") from ex

if blink.auth.check_key_required():
_LOGGER.debug("Attempting a reauth flow")
raise ConfigEntryAuthFailed("Need 2FA for Blink")

hass.data[DOMAIN][entry.entry_id] = blink

if not blink.available:
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/blink/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
try:
await self.sync.async_arm(False)
await self.sync.refresh(force=True)
self.async_write_ha_state()
except asyncio.TimeoutError:
self._attr_available = False

self.async_write_ha_state()

async def async_alarm_arm_away(self, code: str | None = None) -> None:
"""Send arm command."""
try:
await self.sync.async_arm(True)
await self.sync.refresh(force=True)
self.async_write_ha_state()
except asyncio.TimeoutError:
self._attr_available = False

self.async_write_ha_state()
2 changes: 1 addition & 1 deletion homeassistant/components/blink/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def async_setup_entry(
for camera in data.cameras
for description in BINARY_SENSORS_TYPES
]
async_add_entities(entities, update_before_add=True)
async_add_entities(entities)


class BlinkBinarySensor(BinarySensorEntity):
Expand Down
11 changes: 5 additions & 6 deletions homeassistant/components/blink/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
from collections.abc import Mapping
import contextlib
import logging
from typing import Any

Expand Down Expand Up @@ -32,7 +33,7 @@ async def async_setup_entry(
BlinkCamera(data, name, camera) for name, camera in data.cameras.items()
]

async_add_entities(entities, update_before_add=True)
async_add_entities(entities)

platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(SERVICE_TRIGGER, {}, "trigger_camera")
Expand All @@ -44,7 +45,7 @@ class BlinkCamera(Camera):
_attr_has_entity_name = True
_attr_name = None

def __init__(self, data, name, camera):
def __init__(self, data, name, camera) -> None:
"""Initialize a camera."""
super().__init__()
self.data = data
Expand Down Expand Up @@ -91,11 +92,9 @@ def brand(self) -> str | None:

async def trigger_camera(self) -> None:
"""Trigger camera to take a snapshot."""
try:
with contextlib.suppress(asyncio.TimeoutError):
await self._camera.snap_picture()
self.async_schedule_update_ha_state(force_refresh=True)
except asyncio.TimeoutError:
pass
self.async_write_ha_state()

def camera_image(
self, width: int | None = None, height: int | None = None
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/blink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def validate_input(auth: Auth) -> None:
raise Require2FA


async def _send_blink_2fa_pin(hass: HomeAssistant, auth: Auth, pin: str) -> bool:
async def _send_blink_2fa_pin(hass: HomeAssistant, auth: Auth, pin: str | None) -> bool:
"""Send 2FA pin to blink servers."""
blink = Blink(session=async_get_clientsession(hass))
blink.auth = auth
Expand Down Expand Up @@ -127,9 +127,10 @@ async def async_step_2fa(
"""Handle 2FA step."""
errors = {}
if user_input is not None:
pin: str = str(user_input.get(CONF_PIN))
try:
valid_token = await _send_blink_2fa_pin(self.hass, self.auth, pin)
valid_token = await _send_blink_2fa_pin(
self.hass, self.auth, user_input.get(CONF_PIN)
)
except BlinkSetupError:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/blink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def async_setup_entry(
for description in SENSOR_TYPES
]

async_add_entities(entities, update_before_add=True)
async_add_entities(entities)


class BlinkSensor(SensorEntity):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/blink/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ async def test_options_flow(hass: HomeAssistant) -> None:
mock_auth = AsyncMock(
startup=Mock(return_value=True), check_key_required=Mock(return_value=False)
)
mock_blink = AsyncMock()
mock_blink = AsyncMock(cameras=Mock(), sync=Mock())

with patch("homeassistant.components.blink.Auth", return_value=mock_auth), patch(
"homeassistant.components.blink.Blink", return_value=mock_blink
Expand Down

0 comments on commit 9444e1e

Please sign in to comment.