Remove deprecated set_doorbell_message UniFi Protect service (#91523)

* Removes deprecated service

* Linting

* Linting

* More cleanup

* Linting
This commit is contained in:
Christopher Bailey 2023-04-16 23:30:41 -04:00 committed by GitHub
parent 9680161701
commit fdc80e14e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 205 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta
from enum import Enum from enum import Enum
import logging import logging
from typing import Any, Final from typing import Any, Final
@ -25,22 +24,15 @@ from pyunifiprotect.data import (
Sensor, Sensor,
Viewer, Viewer,
) )
import voluptuous as vol
from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, issue_registry as ir
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import ( from homeassistant.helpers.entity_platform import AddEntitiesCallback
AddEntitiesCallback,
async_get_current_platform,
)
from homeassistant.util.dt import utcnow
from .const import ATTR_DURATION, ATTR_MESSAGE, DISPATCH_ADOPT, DOMAIN, TYPE_EMPTY_VALUE from .const import DISPATCH_ADOPT, DOMAIN, TYPE_EMPTY_VALUE
from .data import ProtectData from .data import ProtectData
from .entity import ProtectDeviceEntity, async_all_device_entities from .entity import ProtectDeviceEntity, async_all_device_entities
from .models import PermRequired, ProtectSetableKeysMixin, T from .models import PermRequired, ProtectSetableKeysMixin, T
@ -99,16 +91,6 @@ DEVICE_RECORDING_MODES = [
DEVICE_CLASS_LCD_MESSAGE: Final = "unifiprotect__lcd_message" DEVICE_CLASS_LCD_MESSAGE: Final = "unifiprotect__lcd_message"
SERVICE_SET_DOORBELL_MESSAGE = "set_doorbell_message"
SET_DOORBELL_LCD_MESSAGE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_MESSAGE): cv.string,
vol.Optional(ATTR_DURATION, default=""): cv.string,
}
)
@dataclass @dataclass
class ProtectSelectEntityDescription( class ProtectSelectEntityDescription(
@ -352,12 +334,6 @@ async def async_setup_entry(
) )
async_add_entities(entities) async_add_entities(entities)
platform = async_get_current_platform()
platform.async_register_entity_service(
SERVICE_SET_DOORBELL_MESSAGE,
SET_DOORBELL_LCD_MESSAGE_SCHEMA,
"async_set_doorbell_message",
)
class ProtectSelects(ProtectDeviceEntity, SelectEntity): class ProtectSelects(ProtectDeviceEntity, SelectEntity):
@ -428,43 +404,3 @@ class ProtectSelects(ProtectDeviceEntity, SelectEntity):
if self.entity_description.ufp_enum_type is not None: if self.entity_description.ufp_enum_type is not None:
unifi_value = self.entity_description.ufp_enum_type(unifi_value) unifi_value = self.entity_description.ufp_enum_type(unifi_value)
await self.entity_description.ufp_set(self.device, unifi_value) await self.entity_description.ufp_set(self.device, unifi_value)
async def async_set_doorbell_message(self, message: str, duration: str) -> None:
"""Set LCD Message on Doorbell display."""
ir.async_create_issue(
self.hass,
DOMAIN,
"deprecated_service_set_doorbell_message",
breaks_in_ha_version="2023.3.0",
is_fixable=True,
is_persistent=True,
severity=ir.IssueSeverity.WARNING,
translation_placeholders={
"link": (
"https://www.home-assistant.io/integrations"
"/text#service-textset_value"
)
},
translation_key="deprecated_service_set_doorbell_message",
)
if self.entity_description.device_class != DEVICE_CLASS_LCD_MESSAGE:
raise HomeAssistantError("Not a doorbell text select entity")
assert isinstance(self.device, Camera)
reset_at = None
timeout_msg = ""
if duration.isnumeric():
reset_at = utcnow() + timedelta(minutes=int(duration))
timeout_msg = f" with timeout of {duration} minute(s)"
_LOGGER.debug(
'Setting message for %s to "%s"%s',
self.device.display_name,
message,
timeout_msg,
)
await self.device.set_lcd_text(
DoorbellMessageType.CUSTOM_MESSAGE, message, reset_at=reset_at
)

View File

@ -52,38 +52,6 @@ set_default_doorbell_text:
required: true required: true
selector: selector:
text: text:
set_doorbell_message:
name: Set Doorbell message
description: >
Use to dynamically set the message on a Doorbell LCD screen. This service should only be used to set dynamic messages (i.e. setting the current outdoor temperature on your Doorbell). Static messages should still be set using the Select entity and can be added/removed using the add_doorbell_text/remove_doorbell_text services.
fields:
entity_id:
name: Doorbell Text
description: The Doorbell Text select entity for your Doorbell.
example: "select.front_doorbell_camera_doorbell_text"
required: true
selector:
entity:
integration: unifiprotect
domain: select
message:
name: Message to display
description: The message you would like to display on the LCD screen of your Doorbell. Must be less than 30 characters.
example: "Welcome | 09:23 | 25°C"
required: true
selector:
text:
duration:
name: Duration
description: Number of minutes to display the message for before returning to the default message. The default is to not expire.
example: 5
selector:
number:
min: 1
max: 120
step: 1
mode: slider
unit_of_measurement: minutes
set_chime_paired_doorbells: set_chime_paired_doorbells:
name: Set Chime Paired Doorbells name: Set Chime Paired Doorbells
description: > description: >

View File

@ -3,10 +3,8 @@
from __future__ import annotations from __future__ import annotations
from copy import copy from copy import copy
from datetime import datetime, timedelta from unittest.mock import AsyncMock, Mock
from unittest.mock import AsyncMock, Mock, patch
import pytest
from pyunifiprotect.data import ( from pyunifiprotect.data import (
Camera, Camera,
DoorbellMessageType, DoorbellMessageType,
@ -22,21 +20,15 @@ from pyunifiprotect.data import (
from pyunifiprotect.data.nvr import DoorbellMessage from pyunifiprotect.data.nvr import DoorbellMessage
from homeassistant.components.select import ATTR_OPTIONS from homeassistant.components.select import ATTR_OPTIONS
from homeassistant.components.unifiprotect.const import ( from homeassistant.components.unifiprotect.const import DEFAULT_ATTRIBUTION
ATTR_DURATION,
ATTR_MESSAGE,
DEFAULT_ATTRIBUTION,
)
from homeassistant.components.unifiprotect.select import ( from homeassistant.components.unifiprotect.select import (
CAMERA_SELECTS, CAMERA_SELECTS,
LIGHT_MODE_OFF, LIGHT_MODE_OFF,
LIGHT_SELECTS, LIGHT_SELECTS,
SERVICE_SET_DOORBELL_MESSAGE,
VIEWER_SELECTS, VIEWER_SELECTS,
) )
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_ENTITY_ID, ATTR_OPTION, Platform from homeassistant.const import ATTR_ATTRIBUTION, ATTR_ENTITY_ID, ATTR_OPTION, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .utils import ( from .utils import (
@ -547,99 +539,3 @@ async def test_select_set_option_viewer(
) )
viewer.set_liveview.assert_called_once_with(liveview) viewer.set_liveview.assert_called_once_with(liveview)
async def test_select_service_doorbell_invalid(
hass: HomeAssistant, ufp: MockUFPFixture, doorbell: Camera
) -> None:
"""Test Doorbell Text service (invalid)."""
await init_entry(hass, ufp, [doorbell])
assert_entity_counts(hass, Platform.SELECT, 4, 4)
_, entity_id = ids_from_device_description(
Platform.SELECT, doorbell, CAMERA_SELECTS[1]
)
doorbell.__fields__["set_lcd_text"] = Mock(final=False)
doorbell.set_lcd_text = AsyncMock()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"unifiprotect",
SERVICE_SET_DOORBELL_MESSAGE,
{ATTR_ENTITY_ID: entity_id, ATTR_MESSAGE: "Test"},
blocking=True,
)
assert not doorbell.set_lcd_text.called
async def test_select_service_doorbell_success(
hass: HomeAssistant, ufp: MockUFPFixture, doorbell: Camera
) -> None:
"""Test Doorbell Text service (success)."""
await init_entry(hass, ufp, [doorbell])
assert_entity_counts(hass, Platform.SELECT, 4, 4)
_, entity_id = ids_from_device_description(
Platform.SELECT, doorbell, CAMERA_SELECTS[2]
)
doorbell.__fields__["set_lcd_text"] = Mock(final=False)
doorbell.set_lcd_text = AsyncMock()
await hass.services.async_call(
"unifiprotect",
SERVICE_SET_DOORBELL_MESSAGE,
{
ATTR_ENTITY_ID: entity_id,
ATTR_MESSAGE: "Test",
},
blocking=True,
)
doorbell.set_lcd_text.assert_called_once_with(
DoorbellMessageType.CUSTOM_MESSAGE, "Test", reset_at=None
)
@patch("homeassistant.components.unifiprotect.select.utcnow")
async def test_select_service_doorbell_with_reset(
mock_now,
hass: HomeAssistant,
ufp: MockUFPFixture,
doorbell: Camera,
fixed_now: datetime,
) -> None:
"""Test Doorbell Text service (success with reset time)."""
mock_now.return_value = fixed_now
_, entity_id = ids_from_device_description(
Platform.SELECT, doorbell, CAMERA_SELECTS[2]
)
await init_entry(hass, ufp, [doorbell])
assert_entity_counts(hass, Platform.SELECT, 4, 4)
doorbell.__fields__["set_lcd_text"] = Mock(final=False)
doorbell.set_lcd_text = AsyncMock()
await hass.services.async_call(
"unifiprotect",
SERVICE_SET_DOORBELL_MESSAGE,
{
ATTR_ENTITY_ID: entity_id,
ATTR_MESSAGE: "Test",
ATTR_DURATION: 60,
},
blocking=True,
)
doorbell.set_lcd_text.assert_called_once_with(
DoorbellMessageType.CUSTOM_MESSAGE,
"Test",
reset_at=fixed_now + timedelta(minutes=60),
)