Skip to content

Commit

Permalink
Remove KNOWN_CHROMECAST_INFO_KEY Dependency (#173)
Browse files Browse the repository at this point in the history
* Remove KNOWN_CHROMECAST_INFO_KEY Dependency

 - Mod to make it work with HA 2021.4
 - Added ability to play latest podcast episode (code taken from issues page)

* Remove KNOWN_CHROMECAST_INFO_KEY Dependency

- Mod to make it work with HA 2021.4

* Update __init__.py

* Update sensor.py

* Update manifest.json

* Update manifest.json
icarome authored Apr 11, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6b211ee commit 84f5f7b
Showing 3 changed files with 36 additions and 55 deletions.
55 changes: 21 additions & 34 deletions custom_components/spotcast/__init__.py
Original file line number Diff line number Diff line change
@@ -10,10 +10,8 @@
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.components.cast.media_player import KNOWN_CHROMECAST_INFO_KEY
from homeassistant.components.cast.helpers import ChromeCastZeroconf

__VERSION__ = "3.4.7"
__VERSION__ = "3.5.0"
DOMAIN = "spotcast"

_LOGGER = logging.getLogger(__name__)
@@ -114,12 +112,10 @@ async def run(*args, loop=None, executor=None, **kwargs):
def setup(hass, config):
"""Setup the Spotcast service."""
conf = config[DOMAIN]

sp_dc = conf[CONF_SP_DC]
sp_key = conf[CONF_SP_KEY]
accounts = conf.get(CONF_ACCOUNTS)
spotifyTokenInstances = {}

def get_token_instance(account=None):
""" Get token instance for account """
if account is None or account == "default":
@@ -230,7 +226,17 @@ def play(client, spotify_device_id, uri, random_song, repeat, shuffle, position)
_LOGGER.debug(
"Version: %s, playing URI: %s on device-id: %s", __VERSION__, uri, spotify_device_id
)
if uri.find("track") > 0:
if uri.find("show") > 0:
show_episodes_info = client.show_episodes(uri)
if show_episodes_info and len(show_episodes_info["items"]) > 0:
episode_uri = show_episodes_info["items"][0]["external_urls"]["spotify"]
_LOGGER.debug("Playing episode using uris (latest podcast playlist)= for uri: %s", episode_uri)
client.start_playback(device_id=spotify_device_id, uris=[episode_uri])
elif uri.find("episode") > 0:
_LOGGER.debug("Playing episode using uris= for uri: %s", uri)
client.start_playback(device_id=spotify_device_id, uris=[uri])

elif uri.find("track") > 0:
_LOGGER.debug("Playing track using uris= for uri: %s", uri)
client.start_playback(device_id=spotify_device_id, uris=[uri])
else:
@@ -412,37 +418,17 @@ def __init__(self, hass, call_device_name, call_entity_id):

def getChromecastDevice(self, device_name):
import pychromecast

# Get cast from discovered devices of cast platform
known_devices = self.hass.data.get(KNOWN_CHROMECAST_INFO_KEY, [])

known_devices, browser = pychromecast.get_listed_chromecasts([device_name],zeroconf_instance=ChromeCastZeroconf.get_zeroconf())
browser.stop_discovery()
_LOGGER.debug("Chromecast devices: %s", known_devices)
try:
# HA below 0.113
cast_info = next((x for x in known_devices if x.friendly_name == device_name), None)
except:
cast_info = next(
(
known_devices[x]
for x in known_devices
if known_devices[x].friendly_name == device_name
),
None,
)

_LOGGER.debug("cast info: %s", cast_info)
cast_info = known_devices[0]
except IndexError:
cast_info = None
_LOGGER.info("cast info: %s", cast_info)

if cast_info:
return pychromecast.get_chromecast_from_service(
(
cast_info.services,
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
None,
None,
),
ChromeCastZeroconf.get_zeroconf())
return cast_info
_LOGGER.error(
"Could not find device %s from hass.data",
device_name,
@@ -470,7 +456,8 @@ def getSpotifyDeviceId(self, client):
for device in devices_available["devices"]:
if device["id"] == self.spotifyController.device:
return device["id"]

if not devices_available["devices"] and self.spotifyController.device:
return self.spotifyController.device
_LOGGER.error(
'No device with id "{}" known by Spotify'.format(self.spotifyController.device)
)
4 changes: 2 additions & 2 deletions custom_components/spotcast/manifest.json
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@
"requirements": [
"spotify_token==1.0.0"
],
"homeassistant": "0.113.0",
"version": "3.4.7",
"homeassistant": "2021.4.0",
"version": "3.5.0",
"dependencies": [
],
"after_dependencies": [
32 changes: 13 additions & 19 deletions custom_components/spotcast/sensor.py
Original file line number Diff line number Diff line change
@@ -4,18 +4,17 @@
from homeassistant.helpers.entity import Entity
from homeassistant.util import dt
from homeassistant.const import STATE_OK, STATE_UNKNOWN

from . import DOMAIN, KNOWN_CHROMECAST_INFO_KEY

import homeassistant.components.zeroconf as zc
import pychromecast
from . import DOMAIN
from homeassistant.components.cast.helpers import ChromeCastZeroconf
_LOGGER = logging.getLogger(__name__)

SENSOR_SCAN_INTERVAL_SECS = 10
SENSOR_SCAN_INTERVAL_SECS = 30
SCAN_INTERVAL = timedelta(seconds=SENSOR_SCAN_INTERVAL_SECS)


def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices([ChromecastDevicesSensor(hass)])

class ChromecastDevicesSensor(Entity):

def __init__(self, hass):
@@ -44,19 +43,17 @@ def device_state_attributes(self):

def update(self):
_LOGGER.debug('Getting chromecast devices')

known_devices = self.hass.data.get(KNOWN_CHROMECAST_INFO_KEY, [])

known_devices, browser = pychromecast.get_chromecasts(zeroconf_instance=ChromeCastZeroconf.get_zeroconf())
browser.stop_discovery()
_LOGGER.debug('devices %s', known_devices)

chromecasts = [
{
"host": str(known_devices[k].host),
"port": known_devices[k].port,
"uuid": known_devices[k].uuid,
"model_name": known_devices[k].model_name,
"name": known_devices[k].friendly_name,
'manufacturer': known_devices[k].manufacturer
"host": str(k.socket_client.host),
"port": k.socket_client.port,
"uuid": str(k.uuid),
"model_name": k.model_name,
"name": k.name,
'manufacturer': k.device.manufacturer
}
for k in known_devices
]
@@ -65,6 +62,3 @@ def update(self):
self._attributes['devices'] = chromecasts
self._attributes['last_update'] = dt.now().isoformat('T')
self._state = STATE_OK



0 comments on commit 84f5f7b

Please sign in to comment.