Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes broken live attribute again #12

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions custom_components/youtube/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
ICON = 'mdi:youtube'

BASE_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'
BASE_URL_LIVE = "https://www.youtube.com/channel/{}"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_CHANNEL_ID): cv.string,
Expand Down Expand Up @@ -71,7 +70,7 @@ async def async_update(self):
info.split('<title>')[2].split('</')[0])
url = info.split('<link rel="alternate" href="')[2].split('"/>')[0]
if self.live or url != self.url:
self.live = await is_live(self.channel_id, self._name, self.hass, self.session)
self.live = await is_live(url, self._name, self.hass, self.session)
else:
_LOGGER.debug('%s - Skipping live check', self._name)
self.url = url
Expand Down Expand Up @@ -111,17 +110,16 @@ def device_state_attributes(self):
'live': self.live}


async def is_live(channel_id, name, hass, session):
async def is_live(url, name, hass, session):
"""Return bool if channel is live"""
returnvalue = False
url = BASE_URL_LIVE.format(channel_id)
try:
async with async_timeout.timeout(10, loop=hass.loop):
response = await session.get(url)
info = await response.text()
if 'BADGE_STYLE_TYPE_LIVE_NOW' in info:
if '{"key":"is_viewed_live","value":"True"}' in info:
returnvalue = True
_LOGGER.debug('%s - Channel is live', name)
_LOGGER.debug('%s - Latest Video is live', name)
except Exception as error: # pylint: disable=broad-except
_LOGGER.debug('%s - Could not update - %s', name, error)
return returnvalue