Skip to content

Commit

Permalink
Merge pull request #3 from hudsonbrendon/develop
Browse files Browse the repository at this point in the history
Altera o componente para exibir os programas em tempo real no upcoming-media-card
  • Loading branch information
hudsonbrendon authored Jun 23, 2021
2 parents fd306a9 + 57c0a3c commit e499311
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 54 deletions.
2 changes: 1 addition & 1 deletion custom_components/clarotv/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "clarotv",
"name": "Claro TV",
"version": "0.3.5",
"version": "0.3.6",
"documentation": "https://github.com/hudsonbrendon/sensor.claro.com.br",
"dependencies": [],
"codeowners": ["@hudsonbrendon"],
Expand Down
89 changes: 36 additions & 53 deletions custom_components/clarotv/sensor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import json
import logging
import string
from collections import defaultdict
from datetime import datetime, timedelta

import homeassistant.helpers.config_validation as cv
import pytz
import requests
import voluptuous as vol
from dateutil.relativedelta import relativedelta
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_NAME,
CONF_RESOURCES,
STATE_UNKNOWN,
)
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle

Expand Down Expand Up @@ -44,46 +36,6 @@
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:{}"


def get_data(channel_id, channel_name, channel_logo):
"""Get The request from the api"""
first_date = datetime.now(pytz.timezone("America/Sao_Paulo"))
second_date = first_date + relativedelta(months=1)
programations = []
url = BASE_URL.format(
first_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
second_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
channel_id,
)
response = requests.get(url)
if response.ok:
programations.append(
{
"title_default": "$title",
"line1_default": "",
"line2_default": "$release",
"line3_default": "$runtime",
"line4_default": channel_name,
"icon": "mdi:arrow-down-bold",
}
)

for programation in response.json().get("response").get("docs"):
programations.append(
dict(
title=programation["titulo"],
poster=channel_logo,
fanart=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],
)
)

else:
_LOGGER.error("Cannot perform the request")
return programations


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Setup the currency sensor"""

Expand Down Expand Up @@ -112,7 +64,7 @@ def __init__(self, hass, name, channel_id, channel_name, channel_logo, interval)
self._channel_name = channel_name
self._channel_logo = channel_logo
self._name = name
self._programations = {}
self._programations = []

@property
def name(self):
Expand All @@ -137,6 +89,37 @@ def device_state_attributes(self):

def update(self):
"""Get the latest update fron the api"""
self._programations = get_data(
self._channel_id, self._channel_name, self._channel_logo
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,
)
response = requests.get(url)
if response.ok:
self._programations.append(
{
"title_default": "$title",
"line1_default": "",
"line2_default": "$release",
"line3_default": "$runtime",
"line4_default": self._channel_name,
"icon": "mdi:arrow-down-bold",
}
)

for programation in response.json().get("response").get("docs"):
self._programations.append(
dict(
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],
)
)

else:
_LOGGER.error("{} Request error".format(self._name))

0 comments on commit e499311

Please sign in to comment.