diff --git a/README.md b/README.md index 02308af..787449c 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,22 @@ To get started put all the files from`/custom_components/youtube/` here: ## Example configuration.yaml +Using the old style channel id. + ```yaml sensor: platform: youtube channel_id: UCZ2Ku6wrhdYDHCaBzLaA3bw ``` +Or using new style channel name (_Note: You need to enclose the channel name in quotes!_) + +```yaml +sensor: + platform: youtube + channel_id: '@frenck' +``` + ## Configuration variables key | type | description diff --git a/custom_components/youtube/manifest.json b/custom_components/youtube/manifest.json index b3470cf..bc295d3 100644 --- a/custom_components/youtube/manifest.json +++ b/custom_components/youtube/manifest.json @@ -1,7 +1,7 @@ { "domain": "youtube", "name": "Youtube Sensor", - "version": "0.0.0", + "version": "0.0.1", "documentation": "https://github.com/custom-components/youtube", "dependencies": [], "codeowners": ["@pinkywafer"], diff --git a/custom_components/youtube/sensor.py b/custom_components/youtube/sensor.py index 74af357..e0ef959 100644 --- a/custom_components/youtube/sensor.py +++ b/custom_components/youtube/sensor.py @@ -6,7 +6,6 @@ """ import logging -import async_timeout import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.helpers.aiohttp_client import async_create_clientsession @@ -17,11 +16,11 @@ import html CONF_CHANNEL_ID = 'channel_id' - ICON = 'mdi:youtube' - -BASE_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}' +CHANNEL_URL = "https://www.youtube.com/{}" +RSS_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}' CHANNEL_LIVE_URL = 'https://www.youtube.com/channel/{}' +COOKIES = {"SOCS": "CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg"} PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_CHANNEL_ID): cv.string, @@ -34,22 +33,46 @@ async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): # pylint: disable=unused-argument """Setup sensor platform.""" channel_id = config['channel_id'] + _LOGGER.debug(f'Setting up {channel_id}') session = async_create_clientsession(hass) + if channel_id.startswith('@'): + channel_id = await get_channel_id(session, channel_id) try: - url = BASE_URL.format(channel_id) - async with async_timeout.timeout(10): - response = await session.get(url) - info = await response.text() + url = RSS_URL.format(channel_id) + response = await session.get(url) + info = await response.text() name = info.split('