|
| 1 | +"""The Theme Park Wait Times integration.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from homeassistant.config_entries import ConfigEntry |
| 7 | +from homeassistant.const import Platform |
| 8 | +from homeassistant.core import HomeAssistant |
| 9 | +from homeassistant.helpers import device_registry as dr, entity_registry as er |
| 10 | +from homeassistant.helpers.httpx_client import get_async_client |
| 11 | + |
| 12 | +from .const import ( |
| 13 | + DOMAIN, |
| 14 | + ENTITY_BASE_URL, |
| 15 | + ENTITY_TYPE, |
| 16 | + ID, |
| 17 | + LIVE, |
| 18 | + LIVE_DATA, |
| 19 | + METHOD_GET, |
| 20 | + NAME, |
| 21 | + PARKNAME, |
| 22 | + PARKSLUG, |
| 23 | + QUEUE, |
| 24 | + STANDBY, |
| 25 | + TIME, |
| 26 | + TYPE_ATTRACTION, |
| 27 | + TYPE_SHOW, |
| 28 | + WAIT_TIME, |
| 29 | +) |
| 30 | + |
| 31 | +_LOGGER = logging.getLogger(__name__) |
| 32 | + |
| 33 | +PLATFORMS: list[Platform] = [Platform.SENSOR] |
| 34 | + |
| 35 | + |
| 36 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 37 | + """Set up Theme Park Wait Times from a config entry.""" |
| 38 | + data = hass.data.setdefault(DOMAIN, {}) |
| 39 | + |
| 40 | + api = ThemeParkAPI(hass, entry) |
| 41 | + await api.async_initialize() |
| 42 | + |
| 43 | + data[entry.entry_id] = api |
| 44 | + |
| 45 | + hass.config_entries.async_setup_platforms(entry, PLATFORMS) |
| 46 | + |
| 47 | + device_registry = dr.async_get(hass) |
| 48 | + device_registry.async_get_or_create( |
| 49 | + config_entry_id=entry.entry_id, |
| 50 | + identifiers={(DOMAIN, entry.entry_id)}, |
| 51 | + connections=None, |
| 52 | + name=entry.title, |
| 53 | + ) |
| 54 | + |
| 55 | + return True |
| 56 | + |
| 57 | + |
| 58 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 59 | + """Unload a config entry.""" |
| 60 | + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): |
| 61 | + hass.data[DOMAIN].pop(entry.entry_id) |
| 62 | + |
| 63 | + return unload_ok |
| 64 | + |
| 65 | + |
| 66 | +class ThemeParkAPI: |
| 67 | + """Wrapper for theme parks API.""" |
| 68 | + |
| 69 | + # -- Set in async_initialize -- |
| 70 | + ha_device_registry: dr.DeviceRegistry |
| 71 | + ha_entity_registry: er.EntityRegistry |
| 72 | + |
| 73 | + def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: |
| 74 | + """Initialize the gateway.""" |
| 75 | + self._hass = hass |
| 76 | + self._config_entry = config_entry |
| 77 | + self._parkslug = config_entry.data[PARKSLUG] |
| 78 | + self._parkname = config_entry.data[PARKNAME] |
| 79 | + |
| 80 | + async def async_initialize(self) -> None: |
| 81 | + """Initialize controller and connect radio.""" |
| 82 | + self.ha_device_registry = dr.async_get(self._hass) |
| 83 | + self.ha_entity_registry = er.async_get(self._hass) |
| 84 | + |
| 85 | + async def do_live_lookup(self): |
| 86 | + """Do API lookup of the 'live' page of this park.""" |
| 87 | + _LOGGER.debug("Running do_live_lookup in ThemeParkAPI") |
| 88 | + |
| 89 | + items = await self.do_api_lookup() |
| 90 | + |
| 91 | + def parse_live(item): |
| 92 | + """Parse live data from API.""" |
| 93 | + |
| 94 | + _LOGGER.debug("Parsed API item for: %s", item[NAME]) |
| 95 | + |
| 96 | + name = item[NAME] + " (" + self._parkname + ")" |
| 97 | + |
| 98 | + if "queue" not in item: |
| 99 | + _LOGGER.debug("No queue in item") |
| 100 | + return (item[ID], {ID: item[ID], NAME: name, TIME: None}) |
| 101 | + |
| 102 | + if "STANDBY" not in item[QUEUE]: |
| 103 | + _LOGGER.debug("No STANDBY in item['queue']") |
| 104 | + return (item[ID], {ID: item[ID], NAME: name, TIME: None}) |
| 105 | + |
| 106 | + _LOGGER.debug("Time found") |
| 107 | + return ( |
| 108 | + item[ID], |
| 109 | + { |
| 110 | + ID: item[ID], |
| 111 | + NAME: name, |
| 112 | + TIME: item[QUEUE][STANDBY][WAIT_TIME], |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + return dict(map(parse_live, items)) |
| 117 | + |
| 118 | + async def do_api_lookup(self): |
| 119 | + """Lookup the subpage and subfield in the API.""" |
| 120 | + url = f"{ENTITY_BASE_URL}/{self._parkslug}/{LIVE}" |
| 121 | + |
| 122 | + client = get_async_client(self._hass) |
| 123 | + response = await client.request( |
| 124 | + METHOD_GET, |
| 125 | + url, |
| 126 | + timeout=30, |
| 127 | + follow_redirects=True, |
| 128 | + ) |
| 129 | + |
| 130 | + items_data = response.json() |
| 131 | + |
| 132 | + def filter_item(item): |
| 133 | + return ( |
| 134 | + item[ENTITY_TYPE] == TYPE_SHOW or item[ENTITY_TYPE] == TYPE_ATTRACTION |
| 135 | + ) |
| 136 | + |
| 137 | + return filter(filter_item, items_data[LIVE_DATA]) |
0 commit comments