diff --git a/custom_components/clarotv/manifest.json b/custom_components/clarotv/manifest.json index 4f278b9..a6279f0 100644 --- a/custom_components/clarotv/manifest.json +++ b/custom_components/clarotv/manifest.json @@ -1,7 +1,7 @@ { "domain": "clarotv", "name": "Claro TV", - "version": "0.2.3", + "version": "0.2.4", "documentation": "https://github.com/hudsonbrendon/sensor.claro.com.br", "dependencies": [], "codeowners": ["@hudsonbrendon"], diff --git a/custom_components/clarotv/sensor.py b/custom_components/clarotv/sensor.py index 02a9369..7c7c34a 100755 --- a/custom_components/clarotv/sensor.py +++ b/custom_components/clarotv/sensor.py @@ -5,10 +5,13 @@ https://github.com/hudsonbrendon/sensor.clarotv """ import logging +from datetime import datetime, timedelta import async_timeout import homeassistant.helpers.config_validation as cv +import pytz import voluptuous as vol +from dateutil.relativedelta import relativedelta from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.entity import Entity @@ -16,10 +19,11 @@ CONF_CHANNEL_ID = "channel_id" CONF_CHANNEL_NAME = "channel_name" CONF_CHANNEL_LOGO = "channel_logo" +SCAN_INTERVAL = timedelta(minutes=10) ICON = "mdi:video" -BASE_URL = "https://programacao.claro.com.br/gatekeeper/exibicao/select?q=id_cidade:1&wt=json&rows=10&sort=dh_inicio%20asc&fl=dh_inicio%20st_titulo%20titulo%20id_programa%20id_exibicao&fq=id_canal:{}" +BASE_URL = "https://programacao.claro.com.br/gatekeeper/exibicao/select?q=id_cidade:1&wt=json&sort=dh_inicio%20asc&fl=dh_inicio%20st_titulo%20titulo%20id_programa%20id_exibicao&fq=dh_inicio:%5B{}%20TO%20{}%5D&fq=id_canal:{}" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { @@ -60,7 +64,14 @@ async def async_update(self): """Update sensor.""" _LOGGER.debug("%s - Running update", self._name) try: - url = BASE_URL.format(self._channel_id) + + first_date = datetime.now(pytz.timezone("America/Sao_Paulo")) + second_date = first_date + relativedelta(months=1) + url = BASE_URL.format( + first_date.strftime("%Y-%m-%dT%H:%M:%SZ"), + second_date.strftime("%Y-%m-%dT%H:%M:%SZ"), + self._channel_id, + ) async with async_timeout.timeout(10, loop=self.hass.loop): response = await self.session.get(url) programations = await response.json() @@ -82,9 +93,15 @@ async def async_update(self): title=programation["titulo"], poster=self._channel_logo, fanart=self._channel_logo, - runtime=programation["dh_inicio"].split("T")[1].split("Z")[0], - release=programation["dh_inicio"].split("T")[1].split("Z")[0], - airdate=programation["dh_inicio"].split("T")[1].split("Z")[0], + runtime=programation["dh_inicio"] + .split("T")[1] + .split("Z")[0], + release=programation["dh_inicio"] + .split("T")[1] + .split("Z")[0], + airdate=programation["dh_inicio"] + .split("T")[1] + .split("Z")[0], ) ) @@ -104,7 +121,11 @@ def state(self): @property def programations(self): """Programations.""" - return [i for n, i in enumerate(self._programations) if i not in self._programations[n + 1 :]] + return [ + i + for n, i in enumerate(self._programations) + if i not in self._programations[n + 1 :] + ] @property def icon(self):