1 """The habitica integration."""
3 from http
import HTTPStatus
5 from aiohttp
import ClientResponseError
6 from habitipy.aio
import HabitipyAsync
24 from .const
import CONF_API_USER, DEVELOPER_ID, DOMAIN
25 from .coordinator
import HabiticaDataUpdateCoordinator
26 from .services
import async_setup_services
27 from .types
import HabiticaConfigEntry
29 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
33 Platform.BINARY_SENSOR,
42 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
43 """Set up the Habitica service."""
50 hass: HomeAssistant, config_entry: HabiticaConfigEntry
52 """Set up habitica from a config entry."""
54 class HAHabitipyAsync(HabitipyAsync):
55 """Closure API class to hold session."""
57 def __call__(self, **kwargs):
58 return super().__call__(websession, **kwargs)
60 def _make_headers(self) -> dict[str, str]:
61 headers = super()._make_headers()
63 {
"x-client": f
"{DEVELOPER_ID} - {APPLICATION_NAME} {__version__}"}
68 hass, verify_ssl=config_entry.data.get(CONF_VERIFY_SSL,
True)
71 api = await hass.async_add_executor_job(
74 "url": config_entry.data[CONF_URL],
75 "login": config_entry.data[CONF_API_USER],
76 "password": config_entry.data[CONF_API_KEY],
80 user = await api.user.get(userFields=
"profile")
81 except ClientResponseError
as e:
82 if e.status == HTTPStatus.TOO_MANY_REQUESTS:
84 translation_domain=DOMAIN,
85 translation_key=
"setup_rate_limit_exception",
89 if not config_entry.data.get(CONF_NAME):
90 name = user[
"profile"][
"name"]
91 hass.config_entries.async_update_entry(
93 data={**config_entry.data, CONF_NAME: name},
97 await coordinator.async_config_entry_first_refresh()
99 config_entry.runtime_data = coordinator
100 await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
106 """Unload a config entry."""
107 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, HabiticaConfigEntry config_entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
None async_setup_services(HomeAssistant hass)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)