Remove deprecated mailbox platform (#123740)

* Remove deprecated mailbox platform

* Remove from const

* Remove from type hints

* Remove from pyproject
This commit is contained in:
G Johansson 2024-08-28 16:41:23 +02:00 committed by GitHub
parent 45bb2cdd82
commit dd52f4c84a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 0 additions and 581 deletions

View File

@ -294,7 +294,6 @@ homeassistant.components.london_underground.*
homeassistant.components.lookin.*
homeassistant.components.luftdaten.*
homeassistant.components.madvr.*
homeassistant.components.mailbox.*
homeassistant.components.manual.*
homeassistant.components.map.*
homeassistant.components.mastodon.*

View File

@ -1,291 +0,0 @@
"""Support for Voice mailboxes."""
from __future__ import annotations
import asyncio
from contextlib import suppress
from datetime import timedelta
from http import HTTPStatus
import logging
from typing import Any, Final
from aiohttp import web
from aiohttp.web_exceptions import HTTPNotFound
from homeassistant.components import frontend
from homeassistant.components.http import HomeAssistantView
from homeassistant.config import config_per_platform
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_prepare_setup_platform
_LOGGER = logging.getLogger(__name__)
DOMAIN: Final = "mailbox"
EVENT: Final = "mailbox_updated"
CONTENT_TYPE_MPEG: Final = "audio/mpeg"
CONTENT_TYPE_NONE: Final = "none"
SCAN_INTERVAL = timedelta(seconds=30)
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Track states and offer events for mailboxes."""
mailboxes: list[Mailbox] = []
frontend.async_register_built_in_panel(hass, "mailbox", "mailbox", "mdi:mailbox")
hass.http.register_view(MailboxPlatformsView(mailboxes))
hass.http.register_view(MailboxMessageView(mailboxes))
hass.http.register_view(MailboxMediaView(mailboxes))
hass.http.register_view(MailboxDeleteView(mailboxes))
async def async_setup_platform(
p_type: str,
p_config: ConfigType | None = None,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up a mailbox platform."""
if p_config is None:
p_config = {}
if discovery_info is None:
discovery_info = {}
platform = await async_prepare_setup_platform(hass, config, DOMAIN, p_type)
if platform is None:
_LOGGER.error("Unknown mailbox platform specified")
return
if p_type not in ["asterisk_cdr", "asterisk_mbox", "demo"]:
# Asterisk integration will raise a repair issue themselves
# For demo we don't create one
async_create_issue(
hass,
DOMAIN,
f"deprecated_mailbox_{p_type}",
breaks_in_ha_version="2024.9.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_mailbox_integration",
translation_placeholders={
"integration_domain": p_type,
},
)
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
mailbox = None
try:
if hasattr(platform, "async_get_handler"):
mailbox = await platform.async_get_handler(
hass, p_config, discovery_info
)
elif hasattr(platform, "get_handler"):
mailbox = await hass.async_add_executor_job(
platform.get_handler, hass, p_config, discovery_info
)
else:
raise HomeAssistantError("Invalid mailbox platform.") # noqa: TRY301
if mailbox is None:
_LOGGER.error("Failed to initialize mailbox platform %s", p_type)
return
except Exception:
_LOGGER.exception("Error setting up platform %s", p_type)
return
mailboxes.append(mailbox)
mailbox_entity = MailboxEntity(mailbox)
component = EntityComponent[MailboxEntity](
logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL
)
component.register_shutdown()
await component.async_add_entities([mailbox_entity])
for p_type, p_config in config_per_platform(config, DOMAIN):
if p_type is not None:
hass.async_create_task(
async_setup_platform(p_type, p_config), eager_start=True
)
async def async_platform_discovered(
platform: str, info: DiscoveryInfoType | None
) -> None:
"""Handle for discovered platform."""
await async_setup_platform(platform, discovery_info=info)
discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered)
return True
class MailboxEntity(Entity):
"""Entity for each mailbox platform to provide a badge display."""
def __init__(self, mailbox: Mailbox) -> None:
"""Initialize mailbox entity."""
self.mailbox = mailbox
self.message_count = 0
async def async_added_to_hass(self) -> None:
"""Complete entity initialization."""
@callback
def _mailbox_updated(event: Event) -> None:
self.async_schedule_update_ha_state(True)
self.hass.bus.async_listen(EVENT, _mailbox_updated)
self.async_schedule_update_ha_state(True)
@property
def state(self) -> str:
"""Return the state of the binary sensor."""
return str(self.message_count)
@property
def name(self) -> str:
"""Return the name of the entity."""
return self.mailbox.name
async def async_update(self) -> None:
"""Retrieve messages from platform."""
messages = await self.mailbox.async_get_messages()
self.message_count = len(messages)
class Mailbox:
"""Represent a mailbox device."""
def __init__(self, hass: HomeAssistant, name: str) -> None:
"""Initialize mailbox object."""
self.hass = hass
self.name = name
@callback
def async_update(self) -> None:
"""Send event notification of updated mailbox."""
self.hass.bus.async_fire(EVENT)
@property
def media_type(self) -> str:
"""Return the supported media type."""
raise NotImplementedError
@property
def can_delete(self) -> bool:
"""Return if messages can be deleted."""
return False
@property
def has_media(self) -> bool:
"""Return if messages have attached media files."""
return False
async def async_get_media(self, msgid: str) -> bytes:
"""Return the media blob for the msgid."""
raise NotImplementedError
async def async_get_messages(self) -> list[dict[str, Any]]:
"""Return a list of the current messages."""
raise NotImplementedError
async def async_delete(self, msgid: str) -> bool:
"""Delete the specified messages."""
raise NotImplementedError
class StreamError(Exception):
"""Media streaming exception."""
class MailboxView(HomeAssistantView):
"""Base mailbox view."""
def __init__(self, mailboxes: list[Mailbox]) -> None:
"""Initialize a basic mailbox view."""
self.mailboxes = mailboxes
def get_mailbox(self, platform: str) -> Mailbox:
"""Retrieve the specified mailbox."""
for mailbox in self.mailboxes:
if mailbox.name == platform:
return mailbox
raise HTTPNotFound
class MailboxPlatformsView(MailboxView):
"""View to return the list of mailbox platforms."""
url = "/api/mailbox/platforms"
name = "api:mailbox:platforms"
async def get(self, request: web.Request) -> web.Response:
"""Retrieve list of platforms."""
return self.json(
[
{
"name": mailbox.name,
"has_media": mailbox.has_media,
"can_delete": mailbox.can_delete,
}
for mailbox in self.mailboxes
]
)
class MailboxMessageView(MailboxView):
"""View to return the list of messages."""
url = "/api/mailbox/messages/{platform}"
name = "api:mailbox:messages"
async def get(self, request: web.Request, platform: str) -> web.Response:
"""Retrieve messages."""
mailbox = self.get_mailbox(platform)
messages = await mailbox.async_get_messages()
return self.json(messages)
class MailboxDeleteView(MailboxView):
"""View to delete selected messages."""
url = "/api/mailbox/delete/{platform}/{msgid}"
name = "api:mailbox:delete"
async def delete(self, request: web.Request, platform: str, msgid: str) -> None:
"""Delete items."""
mailbox = self.get_mailbox(platform)
await mailbox.async_delete(msgid)
class MailboxMediaView(MailboxView):
"""View to return a media file."""
url = r"/api/mailbox/media/{platform}/{msgid}"
name = "api:asteriskmbox:media"
async def get(
self, request: web.Request, platform: str, msgid: str
) -> web.Response:
"""Retrieve media."""
mailbox = self.get_mailbox(platform)
with suppress(asyncio.CancelledError, TimeoutError):
async with asyncio.timeout(10):
try:
stream = await mailbox.async_get_media(msgid)
except StreamError as err:
_LOGGER.error("Error getting media: %s", err)
return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)
if stream:
return web.Response(body=stream, content_type=mailbox.media_type)
return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)

View File

@ -1,9 +0,0 @@
{
"domain": "mailbox",
"name": "Mailbox",
"codeowners": [],
"dependencies": ["http"],
"documentation": "https://www.home-assistant.io/integrations/mailbox",
"integration_type": "entity",
"quality_scale": "internal"
}

View File

@ -1,9 +0,0 @@
{
"title": "Mailbox",
"issues": {
"deprecated_mailbox": {
"title": "The mailbox platform is being removed",
"description": "The mailbox platform is being removed. Please report it to the author of the \"{integration_domain}\" custom integration."
}
}
}

View File

@ -60,7 +60,6 @@ class Platform(StrEnum):
LAWN_MOWER = "lawn_mower"
LIGHT = "light"
LOCK = "lock"
MAILBOX = "mailbox"
MEDIA_PLAYER = "media_player"
NOTIFY = "notify"
NUMBER = "number"

View File

@ -2696,16 +2696,6 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.mailbox.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.manual.*]
check_untyped_defs = true
disallow_incomplete_defs = true

View File

@ -1761,39 +1761,6 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
],
),
],
"mailbox": [
ClassTypeHintMatch(
base_class="Mailbox",
matches=[
TypeHintMatch(
function_name="media_type",
return_type="str",
),
TypeHintMatch(
function_name="can_delete",
return_type="bool",
),
TypeHintMatch(
function_name="has_media",
return_type="bool",
),
TypeHintMatch(
function_name="async_get_media",
arg_types={1: "str"},
return_type="bytes",
),
TypeHintMatch(
function_name="async_get_messages",
return_type="list[dict[str, Any]]",
),
TypeHintMatch(
function_name="async_delete",
arg_types={1: "str"},
return_type="bool",
),
],
),
],
"media_player": [
ClassTypeHintMatch(
base_class="Entity",

View File

@ -844,7 +844,6 @@ voluptuous = "vol"
"homeassistant.components.lawn_mower.PLATFORM_SCHEMA" = "LAWN_MOWER_PLATFORM_SCHEMA"
"homeassistant.components.light.PLATFORM_SCHEMA" = "LIGHT_PLATFORM_SCHEMA"
"homeassistant.components.lock.PLATFORM_SCHEMA" = "LOCK_PLATFORM_SCHEMA"
"homeassistant.components.mailbox.PLATFORM_SCHEMA" = "MAILBOX_PLATFORM_SCHEMA"
"homeassistant.components.media_player.PLATFORM_SCHEMA" = "MEDIA_PLAYER_PLATFORM_SCHEMA"
"homeassistant.components.notify.PLATFORM_SCHEMA" = "NOTIFY_PLATFORM_SCHEMA"
"homeassistant.components.number.PLATFORM_SCHEMA" = "NUMBER_PLATFORM_SCHEMA"

View File

@ -1 +0,0 @@
"""The tests for mailbox platforms."""

View File

@ -1,225 +0,0 @@
"""The tests for the mailbox component."""
from datetime import datetime
from hashlib import sha1
from http import HTTPStatus
from typing import Any
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components import mailbox
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import MockModule, mock_integration, mock_platform
from tests.typing import ClientSessionGenerator
MAILBOX_NAME = "TestMailbox"
MEDIA_DATA = b"3f67c4ea33b37d1710f"
MESSAGE_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
def _create_message(idx: int) -> dict[str, Any]:
"""Create a sample message."""
msgtime = dt_util.as_timestamp(datetime(2010, 12, idx + 1, 13, 17, 00))
msgtxt = f"Message {idx + 1}. {MESSAGE_TEXT}"
msgsha = sha1(msgtxt.encode("utf-8")).hexdigest()
return {
"info": {
"origtime": int(msgtime),
"callerid": "John Doe <212-555-1212>",
"duration": "10",
},
"text": msgtxt,
"sha": msgsha,
}
class TestMailbox(mailbox.Mailbox):
"""Test Mailbox, with 10 sample messages."""
# This class doesn't contain any tests! Skip pytest test collection.
__test__ = False
def __init__(self, hass: HomeAssistant, name: str) -> None:
"""Initialize Test mailbox."""
super().__init__(hass, name)
self._messages: dict[str, dict[str, Any]] = {}
for idx in range(10):
msg = _create_message(idx)
msgsha = msg["sha"]
self._messages[msgsha] = msg
@property
def media_type(self) -> str:
"""Return the supported media type."""
return mailbox.CONTENT_TYPE_MPEG
@property
def can_delete(self) -> bool:
"""Return if messages can be deleted."""
return True
@property
def has_media(self) -> bool:
"""Return if messages have attached media files."""
return True
async def async_get_media(self, msgid: str) -> bytes:
"""Return the media blob for the msgid."""
if msgid not in self._messages:
raise mailbox.StreamError("Message not found")
return MEDIA_DATA
async def async_get_messages(self) -> list[dict[str, Any]]:
"""Return a list of the current messages."""
return sorted(
self._messages.values(),
key=lambda item: item["info"]["origtime"], # type: ignore[no-any-return]
reverse=True,
)
async def async_delete(self, msgid: str) -> bool:
"""Delete the specified messages."""
if msgid in self._messages:
del self._messages[msgid]
self.async_update()
return True
class MockMailbox:
"""A mock mailbox platform."""
async def async_get_handler(
self,
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> mailbox.Mailbox:
"""Set up the Test mailbox."""
return TestMailbox(hass, MAILBOX_NAME)
@pytest.fixture
def mock_mailbox(hass: HomeAssistant) -> None:
"""Mock mailbox."""
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.mailbox", MockMailbox())
@pytest.fixture
async def mock_http_client(
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_mailbox: None
) -> TestClient:
"""Start the Home Assistant HTTP component."""
assert await async_setup_component(
hass, mailbox.DOMAIN, {mailbox.DOMAIN: {"platform": "test"}}
)
return await hass_client()
async def test_get_platforms_from_mailbox(mock_http_client: TestClient) -> None:
"""Get platforms from mailbox."""
url = "/api/mailbox/platforms"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.OK
result = await req.json()
assert len(result) == 1
assert result[0].get("name") == "TestMailbox"
async def test_get_messages_from_mailbox(mock_http_client: TestClient) -> None:
"""Get messages from mailbox."""
url = "/api/mailbox/messages/TestMailbox"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.OK
result = await req.json()
assert len(result) == 10
async def test_get_media_from_mailbox(mock_http_client: TestClient) -> None:
"""Get audio from mailbox."""
mp3sha = "7cad61312c7b66f619295be2da8c7ac73b4968f1"
msgtxt = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
msgsha = sha1(msgtxt.encode("utf-8")).hexdigest()
url = f"/api/mailbox/media/TestMailbox/{msgsha}"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.OK
data = await req.read()
assert sha1(data).hexdigest() == mp3sha
async def test_delete_from_mailbox(mock_http_client: TestClient) -> None:
"""Get audio from mailbox."""
msgtxt1 = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
msgtxt2 = "Message 3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
msgsha1 = sha1(msgtxt1.encode("utf-8")).hexdigest()
msgsha2 = sha1(msgtxt2.encode("utf-8")).hexdigest()
for msg in (msgsha1, msgsha2):
url = f"/api/mailbox/delete/TestMailbox/{msg}"
req = await mock_http_client.delete(url)
assert req.status == HTTPStatus.OK
url = "/api/mailbox/messages/TestMailbox"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.OK
result = await req.json()
assert len(result) == 8
async def test_get_messages_from_invalid_mailbox(mock_http_client: TestClient) -> None:
"""Get messages from mailbox."""
url = "/api/mailbox/messages/mailbox.invalid_mailbox"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.NOT_FOUND
async def test_get_media_from_invalid_mailbox(mock_http_client: TestClient) -> None:
"""Get messages from mailbox."""
msgsha = "0000000000000000000000000000000000000000"
url = f"/api/mailbox/media/mailbox.invalid_mailbox/{msgsha}"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.NOT_FOUND
async def test_get_media_from_invalid_msgid(mock_http_client: TestClient) -> None:
"""Get messages from mailbox."""
msgsha = "0000000000000000000000000000000000000000"
url = f"/api/mailbox/media/TestMailbox/{msgsha}"
req = await mock_http_client.get(url)
assert req.status == HTTPStatus.INTERNAL_SERVER_ERROR
async def test_delete_from_invalid_mailbox(mock_http_client: TestClient) -> None:
"""Get audio from mailbox."""
msgsha = "0000000000000000000000000000000000000000"
url = f"/api/mailbox/delete/mailbox.invalid_mailbox/{msgsha}"
req = await mock_http_client.delete(url)
assert req.status == HTTPStatus.NOT_FOUND
async def test_repair_issue_is_created(
hass: HomeAssistant, issue_registry: ir.IssueRegistry, mock_mailbox: None
) -> None:
"""Test repair issue is created."""
assert await async_setup_component(
hass, mailbox.DOMAIN, {mailbox.DOMAIN: {"platform": "test"}}
)
await hass.async_block_till_done()
assert (
mailbox.DOMAIN,
"deprecated_mailbox_test",
) in issue_registry.issues