mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Address Blink late review (#102106)
* Address late review topics * Missing await, optimize config_flow call * Address proper mock for blink * Address typing
This commit is contained in:
parent
e151358aa1
commit
9444e1e2ab
@ -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:
|
||||
|
@ -91,15 +91,17 @@ class BlinkSyncModuleHA(AlarmControlPanelEntity):
|
||||
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()
|
||||
|
@ -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):
|
||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Mapping
|
||||
import contextlib
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@ -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")
|
||||
@ -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
|
||||
@ -91,11 +92,9 @@ class BlinkCamera(Camera):
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
@ -127,9 +127,10 @@ class BlinkConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user