2019-04-03 17:40:03 +02:00
|
|
|
"""Support for Openhome Devices."""
|
2022-01-04 11:30:13 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
import asyncio
|
2022-04-28 17:04:19 +02:00
|
|
|
from collections.abc import Awaitable, Callable, Coroutine
|
2021-05-26 18:39:44 +02:00
|
|
|
import functools
|
2017-02-22 23:01:06 +01:00
|
|
|
import logging
|
2022-04-28 17:04:19 +02:00
|
|
|
from typing import Any, TypeVar
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
import aiohttp
|
|
|
|
from async_upnp_client.client import UpnpError
|
|
|
|
from openhomedevice.device import Device
|
2022-04-28 17:04:19 +02:00
|
|
|
from typing_extensions import Concatenate, ParamSpec
|
2020-05-30 14:40:35 +02:00
|
|
|
import voluptuous as vol
|
2019-12-04 00:47:22 +01:00
|
|
|
|
2022-03-14 20:14:21 +01:00
|
|
|
from homeassistant.components import media_source
|
2022-04-07 15:15:05 +02:00
|
|
|
from homeassistant.components.media_player import (
|
|
|
|
MediaPlayerEntity,
|
|
|
|
MediaPlayerEntityFeature,
|
|
|
|
)
|
2022-03-14 20:14:21 +01:00
|
|
|
from homeassistant.components.media_player.browse_media import (
|
|
|
|
async_process_play_media_url,
|
|
|
|
)
|
2022-04-07 15:15:05 +02:00
|
|
|
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
|
2019-07-31 21:25:30 +02:00
|
|
|
from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
|
2022-01-04 11:30:13 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-05-30 14:40:35 +02:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
2022-01-04 11:30:13 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-05-30 14:40:35 +02:00
|
|
|
|
|
|
|
from .const import ATTR_PIN_INDEX, DATA_OPENHOME, SERVICE_INVOKE_PIN
|
2019-07-31 21:25:30 +02:00
|
|
|
|
2022-04-28 17:04:19 +02:00
|
|
|
_OpenhomeDeviceT = TypeVar("_OpenhomeDeviceT", bound="OpenhomeDevice")
|
|
|
|
_R = TypeVar("_R")
|
|
|
|
_P = ParamSpec("_P")
|
|
|
|
|
2022-04-07 15:15:05 +02:00
|
|
|
SUPPORT_OPENHOME = (
|
|
|
|
MediaPlayerEntityFeature.SELECT_SOURCE
|
|
|
|
| MediaPlayerEntityFeature.TURN_OFF
|
|
|
|
| MediaPlayerEntityFeature.TURN_ON
|
|
|
|
)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-04 11:30:13 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the Openhome platform."""
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2017-04-12 05:10:02 +02:00
|
|
|
if not discovery_info:
|
2020-05-30 14:40:35 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
openhome_data = hass.data.setdefault(DATA_OPENHOME, set())
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
name = discovery_info.get("name")
|
|
|
|
description = discovery_info.get("ssdp_description")
|
2020-05-30 14:40:35 +02:00
|
|
|
|
2017-05-02 22:47:20 +02:00
|
|
|
_LOGGER.info("Openhome device found: %s", name)
|
2020-05-30 14:40:35 +02:00
|
|
|
device = await hass.async_add_executor_job(Device, description)
|
2021-05-26 18:39:44 +02:00
|
|
|
await device.init()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2017-04-12 05:10:02 +02:00
|
|
|
# if device has already been discovered
|
2021-05-26 18:39:44 +02:00
|
|
|
if device.uuid() in openhome_data:
|
2022-01-04 11:30:13 +01:00
|
|
|
return
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2020-05-30 14:40:35 +02:00
|
|
|
entity = OpenhomeDevice(hass, device)
|
|
|
|
|
|
|
|
async_add_entities([entity])
|
2021-05-26 18:39:44 +02:00
|
|
|
openhome_data.add(device.uuid())
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-03 18:34:28 +02:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2020-05-30 14:40:35 +02:00
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_INVOKE_PIN,
|
|
|
|
{vol.Required(ATTR_PIN_INDEX): cv.positive_int},
|
2021-05-26 18:39:44 +02:00
|
|
|
"async_invoke_pin",
|
2020-05-30 14:40:35 +02:00
|
|
|
)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
|
2022-04-28 17:04:19 +02:00
|
|
|
def catch_request_errors() -> Callable[
|
|
|
|
[Callable[Concatenate[_OpenhomeDeviceT, _P], Awaitable[_R]]],
|
|
|
|
Callable[Concatenate[_OpenhomeDeviceT, _P], Coroutine[Any, Any, _R | None]],
|
|
|
|
]:
|
2021-05-26 18:39:44 +02:00
|
|
|
"""Catch asyncio.TimeoutError, aiohttp.ClientError, UpnpError errors."""
|
|
|
|
|
2022-04-28 17:04:19 +02:00
|
|
|
def call_wrapper(
|
|
|
|
func: Callable[Concatenate[_OpenhomeDeviceT, _P], Awaitable[_R]]
|
|
|
|
) -> Callable[Concatenate[_OpenhomeDeviceT, _P], Coroutine[Any, Any, _R | None]]:
|
2021-05-26 18:39:44 +02:00
|
|
|
"""Call wrapper for decorator."""
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
2022-04-28 17:04:19 +02:00
|
|
|
async def wrapper(
|
|
|
|
self: _OpenhomeDeviceT, *args: _P.args, **kwargs: _P.kwargs
|
|
|
|
) -> _R | None:
|
2021-05-26 18:39:44 +02:00
|
|
|
"""Catch asyncio.TimeoutError, aiohttp.ClientError, UpnpError errors."""
|
|
|
|
try:
|
|
|
|
return await func(self, *args, **kwargs)
|
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError, UpnpError):
|
|
|
|
_LOGGER.error("Error during call %s", func.__name__)
|
2022-04-28 17:04:19 +02:00
|
|
|
return None
|
2021-05-26 18:39:44 +02:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return call_wrapper
|
|
|
|
|
|
|
|
|
2020-04-25 18:00:57 +02:00
|
|
|
class OpenhomeDevice(MediaPlayerEntity):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Representation of an Openhome device."""
|
|
|
|
|
|
|
|
def __init__(self, hass, device):
|
|
|
|
"""Initialise the Openhome device."""
|
|
|
|
self.hass = hass
|
|
|
|
self._device = device
|
|
|
|
self._track_information = {}
|
|
|
|
self._in_standby = None
|
|
|
|
self._transport_state = None
|
|
|
|
self._volume_level = None
|
|
|
|
self._volume_muted = None
|
2022-04-07 15:15:05 +02:00
|
|
|
self._attr_supported_features = SUPPORT_OPENHOME
|
2020-04-04 23:14:47 +02:00
|
|
|
self._source_names = []
|
2017-02-22 23:01:06 +01:00
|
|
|
self._source_index = {}
|
|
|
|
self._source = {}
|
|
|
|
self._name = None
|
|
|
|
self._state = STATE_PLAYING
|
2021-05-26 18:39:44 +02:00
|
|
|
self._available = True
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Device is available."""
|
|
|
|
return self._available
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
async def async_update(self):
|
|
|
|
"""Update state of device."""
|
|
|
|
try:
|
|
|
|
self._in_standby = await self._device.is_in_standby()
|
|
|
|
self._transport_state = await self._device.transport_state()
|
|
|
|
self._track_information = await self._device.track_info()
|
|
|
|
self._source = await self._device.source()
|
|
|
|
self._name = await self._device.room()
|
2022-04-07 15:15:05 +02:00
|
|
|
self._attr_supported_features = SUPPORT_OPENHOME
|
2021-05-26 18:39:44 +02:00
|
|
|
source_index = {}
|
|
|
|
source_names = []
|
|
|
|
|
|
|
|
if self._device.volume_enabled:
|
2022-04-07 15:15:05 +02:00
|
|
|
self._attr_supported_features |= (
|
|
|
|
MediaPlayerEntityFeature.VOLUME_STEP
|
|
|
|
| MediaPlayerEntityFeature.VOLUME_MUTE
|
|
|
|
| MediaPlayerEntityFeature.VOLUME_SET
|
2021-05-26 18:39:44 +02:00
|
|
|
)
|
|
|
|
self._volume_level = await self._device.volume() / 100.0
|
|
|
|
self._volume_muted = await self._device.is_muted()
|
|
|
|
|
|
|
|
for source in await self._device.sources():
|
|
|
|
source_names.append(source["name"])
|
|
|
|
source_index[source["name"]] = source["index"]
|
|
|
|
|
|
|
|
self._source_index = source_index
|
|
|
|
self._source_names = source_names
|
|
|
|
|
|
|
|
if self._source["type"] == "Radio":
|
2022-04-07 15:15:05 +02:00
|
|
|
self._attr_supported_features |= (
|
|
|
|
MediaPlayerEntityFeature.STOP
|
|
|
|
| MediaPlayerEntityFeature.PLAY
|
|
|
|
| MediaPlayerEntityFeature.PLAY_MEDIA
|
|
|
|
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
2021-05-26 18:39:44 +02:00
|
|
|
)
|
|
|
|
if self._source["type"] in ("Playlist", "Spotify"):
|
2022-04-07 15:15:05 +02:00
|
|
|
self._attr_supported_features |= (
|
|
|
|
MediaPlayerEntityFeature.PREVIOUS_TRACK
|
|
|
|
| MediaPlayerEntityFeature.NEXT_TRACK
|
|
|
|
| MediaPlayerEntityFeature.PAUSE
|
|
|
|
| MediaPlayerEntityFeature.PLAY
|
|
|
|
| MediaPlayerEntityFeature.PLAY_MEDIA
|
|
|
|
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
2021-05-26 18:39:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if self._in_standby:
|
|
|
|
self._state = STATE_OFF
|
|
|
|
elif self._transport_state == "Paused":
|
|
|
|
self._state = STATE_PAUSED
|
|
|
|
elif self._transport_state in ("Playing", "Buffering"):
|
|
|
|
self._state = STATE_PLAYING
|
|
|
|
elif self._transport_state == "Stopped":
|
|
|
|
self._state = STATE_IDLE
|
|
|
|
else:
|
|
|
|
# Device is playing an external source with no transport controls
|
|
|
|
self._state = STATE_PLAYING
|
|
|
|
|
|
|
|
self._available = True
|
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError, UpnpError):
|
|
|
|
self._available = False
|
|
|
|
|
|
|
|
@catch_request_errors()
|
|
|
|
async def async_turn_on(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Bring device out of standby."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.set_standby(False)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_turn_off(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Put device in standby."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.set_standby(True)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_play_media(self, media_type, media_id, **kwargs):
|
2020-01-18 18:37:39 +01:00
|
|
|
"""Send the play_media command to the media player."""
|
2022-03-14 20:14:21 +01:00
|
|
|
if media_source.is_media_source_id(media_id):
|
|
|
|
media_type = MEDIA_TYPE_MUSIC
|
|
|
|
play_item = await media_source.async_resolve_media(self.hass, media_id)
|
|
|
|
media_id = play_item.url
|
|
|
|
|
2021-03-20 01:27:04 +01:00
|
|
|
if media_type != MEDIA_TYPE_MUSIC:
|
2020-01-18 18:37:39 +01:00
|
|
|
_LOGGER.error(
|
|
|
|
"Invalid media type %s. Only %s is supported",
|
|
|
|
media_type,
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
)
|
|
|
|
return
|
2022-03-14 20:14:21 +01:00
|
|
|
|
|
|
|
media_id = async_process_play_media_url(self.hass, media_id)
|
|
|
|
|
2020-01-18 18:37:39 +01:00
|
|
|
track_details = {"title": "Home Assistant", "uri": media_id}
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.play_media(track_details)
|
2020-01-18 18:37:39 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_media_pause(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Send pause command."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.pause()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_media_stop(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Send stop command."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.stop()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_media_play(self):
|
2017-09-23 17:15:46 +02:00
|
|
|
"""Send play command."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.play()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_media_next_track(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Send next track command."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.skip(1)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_media_previous_track(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Send previous track command."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.skip(-1)
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_select_source(self, source):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Select input source."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.set_source(self._source_index[source])
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_invoke_pin(self, pin):
|
2020-05-30 14:40:35 +02:00
|
|
|
"""Invoke pin."""
|
2021-05-26 18:39:44 +02:00
|
|
|
try:
|
|
|
|
if self._device.pins_enabled:
|
|
|
|
await self._device.invoke_pin(pin)
|
|
|
|
else:
|
|
|
|
_LOGGER.error("Pins service not supported")
|
|
|
|
except (UpnpError):
|
|
|
|
_LOGGER.error("Error invoking pin %s", pin)
|
2020-05-30 14:40:35 +02:00
|
|
|
|
2017-02-22 23:01:06 +01:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-03-03 19:23:55 +01:00
|
|
|
"""Return a unique ID."""
|
2021-05-26 18:39:44 +02:00
|
|
|
return self._device.uuid()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return self._source_names
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_image_url(self):
|
|
|
|
"""Image url of current playing media."""
|
2019-07-31 21:25:30 +02:00
|
|
|
return self._track_information.get("albumArtwork")
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_artist(self):
|
|
|
|
"""Artist of current playing media, music track only."""
|
2021-10-21 08:27:42 +02:00
|
|
|
if artists := self._track_information.get("artist"):
|
2017-06-09 06:41:24 +02:00
|
|
|
return artists[0]
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_album_name(self):
|
|
|
|
"""Album name of current playing media, music track only."""
|
2019-07-31 21:25:30 +02:00
|
|
|
return self._track_information.get("albumTitle")
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_title(self):
|
|
|
|
"""Title of current playing media."""
|
2019-07-31 21:25:30 +02:00
|
|
|
return self._track_information.get("title")
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Name of the current input source."""
|
2019-07-31 21:25:30 +02:00
|
|
|
return self._source.get("name")
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Volume level of the media player (0..1)."""
|
2020-03-12 09:54:25 +01:00
|
|
|
return self._volume_level
|
2017-02-22 23:01:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Return true if volume is muted."""
|
|
|
|
return self._volume_muted
|
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_volume_up(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Volume up media player."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.increase_volume()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_volume_down(self):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Volume down media player."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.decrease_volume()
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_set_volume_level(self, volume):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Set volume level, range 0..1."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.set_volume(int(volume * 100))
|
2017-02-22 23:01:06 +01:00
|
|
|
|
2021-05-26 18:39:44 +02:00
|
|
|
@catch_request_errors()
|
|
|
|
async def async_mute_volume(self, mute):
|
2017-02-22 23:01:06 +01:00
|
|
|
"""Mute (true) or unmute (false) media player."""
|
2021-05-26 18:39:44 +02:00
|
|
|
await self._device.set_mute(mute)
|
2022-03-14 20:14:21 +01:00
|
|
|
|
|
|
|
async def async_browse_media(self, media_content_type=None, media_content_id=None):
|
|
|
|
"""Implement the websocket media browsing helper."""
|
|
|
|
return await media_source.async_browse_media(
|
|
|
|
self.hass,
|
|
|
|
media_content_id,
|
|
|
|
content_filter=lambda item: item.media_content_type.startswith("audio/"),
|
|
|
|
)
|