Skip to content

Commit

Permalink
Merge pull request #12 from hudsonbrendon/feature/refactor
Browse files Browse the repository at this point in the history
fix: update ci
  • Loading branch information
hudsonbrendon authored Apr 26, 2023
2 parents b233c27 + 39ccad5 commit 6496aef
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 29 deletions.
86 changes: 71 additions & 15 deletions custom_components/ingresso/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,84 @@
from .const import DOMAIN
import logging
from typing import Optional, Union

import requests
from homeassistant import config_entries, core
from homeassistant.const import Platform
from homeassistant.exceptions import ConfigEntryAuthFailed
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

from .const import BASE_URL, CONF_CITY_ID, CONF_CITY_NAME, CONF_PARTNERSHIP, DOMAIN

async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
"""Set up the Ingresso component."""
hass.data.setdefault(DOMAIN, {})
PLATFORMS = [Platform.SENSOR]

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
)

return True
_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data
if await get_movies(
hass=hass,
city_id=entry.data.get(CONF_CITY_ID),
partnership=entry.data.get(CONF_PARTNERSHIP),
):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data

for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
else:
raise ConfigEntryAuthFailed("Invalid credentials")
return True


async def async_unload_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
) -> bool:
"""Unload a config entry."""

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok


async def async_migrate_entry(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
):
hass.config_entries.async_update_entry(
config_entry,
data={
CONF_CITY_ID: config_entry.data.get(CONF_CITY_ID),
CONF_CITY_NAME: config_entry.data.get(CONF_CITY_NAME),
CONF_PARTNERSHIP: config_entry.data.get(CONF_PARTNERSHIP),
},
)

return True


async def get_movies(
hass, city_id: int, partnership: str
) -> Union[dict, Optional[None]]:
def get():
url = BASE_URL.format(city_id, partnership)
retry_strategy = Retry(
total=3,
status_forcelist=[400, 401, 404, 500, 502, 503, 504],
method_whitelist=["GET"],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)

return http.get(url, headers={"User-Agent": "Mozilla/5.0"})

response = await hass.async_add_executor_job(get)
_LOGGER.debug("API Response movies: %s", response.json())

if response.ok:
return response.json()
return None
51 changes: 38 additions & 13 deletions custom_components/ingresso/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from collections.abc import Mapping

import voluptuous as vol
from homeassistant import config_entries
from .const import CONF_CITY_ID, CONF_CITY_NAME, CONF_PARTNERSHIP
from .const import DOMAIN

from . import get_movies
from .const import CONF_CITY_ID, CONF_CITY_NAME, CONF_PARTNERSHIP, DOMAIN

DATA_SCHEMA = vol.Schema(
DATA_SCHEMA: vol.Schema = vol.Schema(
{
vol.Required(CONF_CITY_ID): int,
vol.Required(CONF_CITY_NAME): str,
Expand All @@ -14,21 +16,44 @@


class IngressoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Ingresso.com config flow."""
"""Ingresso config flow."""

def __init__(self) -> None:
"""Initialize Ingresso config flow."""
self.city_id: str
self.city_name: str
self.partnership: str

async def async_step_user(self, user_input=None):
errors = {}

if user_input is not None:
return self.async_create_entry(
title=f"{user_input[CONF_PARTNERSHIP].capitalize()} {user_input[CONF_CITY_NAME].capitalize()}",
data={
CONF_CITY_ID: user_input[CONF_CITY_ID],
CONF_CITY_NAME: user_input[CONF_CITY_NAME],
CONF_PARTNERSHIP: user_input[CONF_PARTNERSHIP],
},
)
if await get_movies(
hass=self.hass,
city_id=user_input.get(CONF_CITY_ID),
partnership=user_input.get(CONF_PARTNERSHIP),
):
self.city_id = user_input.get(CONF_CITY_ID)
self.city_name = user_input.get(CONF_CITY_NAME)
self.partnership = user_input.get(CONF_PARTNERSHIP)

return self.async_create_entry(
title=f"{self.partnership.capitalize()} {self.city_name.capitalize()}",
data={
CONF_CITY_ID: self.city_id,
CONF_CITY_NAME: self.city_name,
CONF_PARTNERSHIP: self.partnership,
},
)

errors[CONF_CITY_ID] = "auth_error"
errors[CONF_PARTNERSHIP] = "auth_error"

return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
step_id="user",
data_schema=self.add_suggested_values_to_schema(
DATA_SCHEMA,
user_input,
),
errors=errors,
)
1 change: 0 additions & 1 deletion custom_components/ingresso/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import timedelta


BASE_URL = "https://api-content.ingresso.com/v0/templates/nowplaying/{}?partnership={}"
DEFAULT_POSTER = "https://www.promoview.com.br/uploads/2019/01/images/07.01.2019/ingresso.comlogo.jpg"
ICON = "mdi:ticket"
Expand Down
14 changes: 14 additions & 0 deletions custom_components/ingresso/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"config": {
"step": {
"user": {
"description": "Enter your movie theater information:",
"data": {
"city_id": "City ID",
"city_name": "City name",
"partnership": "Partnership"
}
}
}
}
}
14 changes: 14 additions & 0 deletions custom_components/ingresso/translations/pt-BR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"config": {
"step": {
"user": {
"description": "Informe os dados do cinema:",
"data": {
"city_id": "ID da cidade",
"city_name": "Nome da cidade",
"partnership": "Cinema"
}
}
}
}
}

0 comments on commit 6496aef

Please sign in to comment.