Skip to content

Commit

Permalink
add stream_start attribute (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkywafer committed May 5, 2021
1 parent a4746a8 commit 896d479
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ key | type | description
* stars: The 'stars' recieved on youtube. (This is all reactions both 👍 and 👎 combined)
* views: the number of video views
* stream: If the video was streamed live
* stream_start: datetime of the start of a live stream
* live: If the video is live now
* channel_is_live: If any video on the channel is live
* channel_image: URL of the channel logo image
Expand Down
8 changes: 6 additions & 2 deletions custom_components/youtube/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(self, channel_id, name, session):
self.channel_live = False
self.channel_image = None
self.expiry = parse('01 Jan 1900 00:00:00 UTC')
self.stream_start = None

async def async_update(self):
"""Update sensor."""
Expand All @@ -83,7 +84,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.stream, self.live = await is_live(url, self._name, self.hass, self.session)
self.stream, self.live, self.stream_start = 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 @@ -127,6 +128,7 @@ def device_state_attributes(self):
'stars': self.stars,
'views': self.views,
'stream': self.stream,
'stream_start': self.stream_start,
'live': self.live,
'channel_is_live': self.channel_live,
'channel_image': self.channel_image}
Expand All @@ -135,18 +137,20 @@ async def is_live(url, name, hass, session):
"""Return bool if video is stream and bool if video is live"""
live = False
stream = False
start = None
try:
async with async_timeout.timeout(10, loop=hass.loop):
response = await session.get(url, cookies=dict(CONSENT="YES+cb"))
info = await response.text()
if 'isLiveBroadcast' in info:
stream = True
start = parse(info.split('startDate" content="')[1].split('"')[0])
if 'endDate' not in info:
live = True
_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 stream, live
return stream, live, start

async def is_channel_live(url, name, hass, session):
"""Return bool if channel is live"""
Expand Down
1 change: 1 addition & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ key | type | description
* stars: The 'stars' recieved on youtube. (This is all reactions both 👍 and 👎 combined)
* views: the number of video views
* stream: If the video was streamed live
* stream_start: datetime of the start of a live stream
* live: If the video is live now
* channel_is_live: If any video on the channel is live
* channel_image: URL of the channel logo image

0 comments on commit 896d479

Please sign in to comment.