Skip to content

Commit

Permalink
Revert "Remove KNOWN_CHROMECAST_INFO_KEY Dependency (#173)" (#175)
Browse files Browse the repository at this point in the history
This reverts commit 84f5f7b.
  • Loading branch information
fondberg authored Apr 11, 2021
1 parent 98192c9 commit 5aedf71
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
55 changes: 34 additions & 21 deletions custom_components/spotcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
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.5.0"

__VERSION__ = "3.4.7"
DOMAIN = "spotcast"

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,10 +114,12 @@ 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":
Expand Down Expand Up @@ -226,17 +230,7 @@ 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("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:
if 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:
Expand Down Expand Up @@ -418,17 +412,37 @@ def __init__(self, hass, call_device_name, call_entity_id):

def getChromecastDevice(self, device_name):
import pychromecast
known_devices, browser = pychromecast.get_listed_chromecasts([device_name],zeroconf_instance=ChromeCastZeroconf.get_zeroconf())
browser.stop_discovery()

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

_LOGGER.debug("Chromecast devices: %s", known_devices)
try:
cast_info = known_devices[0]
except IndexError:
cast_info = None
_LOGGER.info("cast info: %s", cast_info)
# 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)

if cast_info:
return 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())
_LOGGER.error(
"Could not find device %s from hass.data",
device_name,
Expand Down Expand Up @@ -456,8 +470,7 @@ 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)
)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/spotcast/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"requirements": [
"spotify_token==1.0.0"
],
"homeassistant": "2021.4.0",
"version": "3.5.0",
"homeassistant": "0.113.0",
"version": "3.4.7",
"dependencies": [
],
"after_dependencies": [
Expand Down
32 changes: 19 additions & 13 deletions custom_components/spotcast/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
from homeassistant.helpers.entity import Entity
from homeassistant.util import dt
from homeassistant.const import STATE_OK, STATE_UNKNOWN
import homeassistant.components.zeroconf as zc
import pychromecast
from . import DOMAIN
from homeassistant.components.cast.helpers import ChromeCastZeroconf

from . import DOMAIN, KNOWN_CHROMECAST_INFO_KEY

_LOGGER = logging.getLogger(__name__)
SENSOR_SCAN_INTERVAL_SECS = 30

SENSOR_SCAN_INTERVAL_SECS = 10
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):
Expand Down Expand Up @@ -43,17 +44,19 @@ def device_state_attributes(self):

def update(self):
_LOGGER.debug('Getting chromecast devices')
known_devices, browser = pychromecast.get_chromecasts(zeroconf_instance=ChromeCastZeroconf.get_zeroconf())
browser.stop_discovery()

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

_LOGGER.debug('devices %s', known_devices)

chromecasts = [
{
"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
"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
}
for k in known_devices
]
Expand All @@ -62,3 +65,6 @@ def update(self):
self._attributes['devices'] = chromecasts
self._attributes['last_update'] = dt.now().isoformat('T')
self._state = STATE_OK



0 comments on commit 5aedf71

Please sign in to comment.