Skip to content

Commit

Permalink
Fixes media player states. Adds search service. And bump to 0.1.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkristian committed Aug 12, 2021
2 parents 0309768 + c17b57f commit 227e33c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
5 changes: 4 additions & 1 deletion custom_components/casatunes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ class CasaTunesDeviceEntity(CasaTunesEntity):

@property
def device_info(self) -> DeviceInfo:
"""Return device information about this CasaTunes instance."""
"""Return device information about this CasaTunes device."""
if self._device_id is None:
return None

return {
"identifiers": {(DOMAIN, self._device_id)},
"manufacturer": "CasaTunes",
Expand Down
6 changes: 6 additions & 0 deletions custom_components/casatunes/const.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
"""Constants for the CasaTunes integration."""
DOMAIN = "casatunes"

# Attributes
ATTR_KEYWORD = "keyword"

# Services
SERVICE_SEARCH = "search"
2 changes: 1 addition & 1 deletion custom_components/casatunes/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/jonkristian/casatunes",
"issue_tracker": "https://github.com/jonkristian/casatunes/issues",
"version": "0.1.1",
"version": "0.1.2",
"requirements": [
"pycasatunes==0.1.1"
],
Expand Down
22 changes: 19 additions & 3 deletions custom_components/casatunes/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging

import voluptuous as vol

from homeassistant.util.dt import utcnow
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

Expand Down Expand Up @@ -43,8 +45,9 @@
STATE_PLAYING,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import entity_platform

from .const import DOMAIN
from .const import ATTR_KEYWORD, DOMAIN, SERVICE_SEARCH
from .browse_media import build_item_response
from . import CasaTunesDataUpdateCoordinator, CasaTunesDeviceEntity

Expand All @@ -69,6 +72,8 @@

STATUS_TO_STATES = {0: STATE_IDLE, 1: STATE_PAUSED, 2: STATE_PLAYING, 3: STATE_ON}

SEARCH_SCHEMA = {vol.Required(ATTR_KEYWORD): str}

_LOGGER = logging.getLogger(__name__)


Expand All @@ -85,6 +90,13 @@ async def async_setup_entry(hass, entry, async_add_entities):

async_add_entities(media_players)

platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_SEARCH,
SEARCH_SCHEMA,
"search",
)


class CasaTunesMediaPlayer(CasaTunesDeviceEntity, MediaPlayerEntity):
"""Representation of a CasaTunes media player on the network."""
Expand Down Expand Up @@ -168,7 +180,7 @@ def name(self) -> str | None:
@property
def state(self) -> str | None:
"""Return the state of the device."""
if self.zone and self.zone.Power is not False:
if self.zone.Power is True:
state = self.coordinator.data.nowplaying[self.zone.SourceID].Status
return STATUS_TO_STATES.get(state, None)
else:
Expand Down Expand Up @@ -420,4 +432,8 @@ async def async_play_media(self, media_type, media_id, **kwargs):

async def async_clear_playlist(self):
"""Send the media player the command for clear playlist."""
await self._player.async_clear_playlist()
await self._player.async_clear_playlist()

async def search(self, keyword):
"""Emulate opening the search screen and entering the search keyword."""
await self.coordinator.data.search_media(self.zone_id, keyword)
15 changes: 15 additions & 0 deletions custom_components/casatunes/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
search:
name: Search
description: Emulates opening the search screen and entering the search keyword.
target:
entity:
integration: casatunes
domain: media_player
fields:
keyword:
name: Keyword
description: The keyword to search for.
required: true
example: "Pearl Jam"
selector:
text:

0 comments on commit 227e33c

Please sign in to comment.