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
7 changed files with 20 additions and 17 deletions
|
@ -87,12 +87,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await blink.start()
|
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:
|
except (ClientError, asyncio.TimeoutError) as ex:
|
||||||
raise ConfigEntryNotReady("Can not connect to host") from 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
|
hass.data[DOMAIN][entry.entry_id] = blink
|
||||||
|
|
||||||
if not blink.available:
|
if not blink.available:
|
||||||
|
|
|
@ -91,15 +91,17 @@ class BlinkSyncModuleHA(AlarmControlPanelEntity):
|
||||||
try:
|
try:
|
||||||
await self.sync.async_arm(False)
|
await self.sync.async_arm(False)
|
||||||
await self.sync.refresh(force=True)
|
await self.sync.refresh(force=True)
|
||||||
self.async_write_ha_state()
|
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
||||||
"""Send arm command."""
|
"""Send arm command."""
|
||||||
try:
|
try:
|
||||||
await self.sync.async_arm(True)
|
await self.sync.async_arm(True)
|
||||||
await self.sync.refresh(force=True)
|
await self.sync.refresh(force=True)
|
||||||
self.async_write_ha_state()
|
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
|
@ -52,7 +52,7 @@ async def async_setup_entry(
|
||||||
for camera in data.cameras
|
for camera in data.cameras
|
||||||
for description in BINARY_SENSORS_TYPES
|
for description in BINARY_SENSORS_TYPES
|
||||||
]
|
]
|
||||||
async_add_entities(entities, update_before_add=True)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class BlinkBinarySensor(BinarySensorEntity):
|
class BlinkBinarySensor(BinarySensorEntity):
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ async def async_setup_entry(
|
||||||
BlinkCamera(data, name, camera) for name, camera in data.cameras.items()
|
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 = entity_platform.async_get_current_platform()
|
||||||
platform.async_register_entity_service(SERVICE_TRIGGER, {}, "trigger_camera")
|
platform.async_register_entity_service(SERVICE_TRIGGER, {}, "trigger_camera")
|
||||||
|
@ -44,7 +45,7 @@ class BlinkCamera(Camera):
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
|
|
||||||
def __init__(self, data, name, camera):
|
def __init__(self, data, name, camera) -> None:
|
||||||
"""Initialize a camera."""
|
"""Initialize a camera."""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.data = data
|
self.data = data
|
||||||
|
@ -91,11 +92,9 @@ class BlinkCamera(Camera):
|
||||||
|
|
||||||
async def trigger_camera(self) -> None:
|
async def trigger_camera(self) -> None:
|
||||||
"""Trigger camera to take a snapshot."""
|
"""Trigger camera to take a snapshot."""
|
||||||
try:
|
with contextlib.suppress(asyncio.TimeoutError):
|
||||||
await self._camera.snap_picture()
|
await self._camera.snap_picture()
|
||||||
self.async_schedule_update_ha_state(force_refresh=True)
|
self.async_write_ha_state()
|
||||||
except asyncio.TimeoutError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def camera_image(
|
def camera_image(
|
||||||
self, width: int | None = None, height: int | None = None
|
self, width: int | None = None, height: int | None = None
|
||||||
|
|
|
@ -60,7 +60,7 @@ async def validate_input(auth: Auth) -> None:
|
||||||
raise Require2FA
|
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."""
|
"""Send 2FA pin to blink servers."""
|
||||||
blink = Blink(session=async_get_clientsession(hass))
|
blink = Blink(session=async_get_clientsession(hass))
|
||||||
blink.auth = auth
|
blink.auth = auth
|
||||||
|
@ -127,9 +127,10 @@ class BlinkConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle 2FA step."""
|
"""Handle 2FA step."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
pin: str = str(user_input.get(CONF_PIN))
|
|
||||||
try:
|
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:
|
except BlinkSetupError:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
|
|
|
@ -56,7 +56,7 @@ async def async_setup_entry(
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
]
|
]
|
||||||
|
|
||||||
async_add_entities(entities, update_before_add=True)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class BlinkSensor(SensorEntity):
|
class BlinkSensor(SensorEntity):
|
||||||
|
|
|
@ -271,7 +271,7 @@ async def test_options_flow(hass: HomeAssistant) -> None:
|
||||||
mock_auth = AsyncMock(
|
mock_auth = AsyncMock(
|
||||||
startup=Mock(return_value=True), check_key_required=Mock(return_value=False)
|
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(
|
with patch("homeassistant.components.blink.Auth", return_value=mock_auth), patch(
|
||||||
"homeassistant.components.blink.Blink", return_value=mock_blink
|
"homeassistant.components.blink.Blink", return_value=mock_blink
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue