Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The surepetcare integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from surepy.enums import Location
9 from surepy.exceptions import SurePetcareAuthenticationError, SurePetcareError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import Platform
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
16 from homeassistant.helpers import config_validation as cv
17 
18 from .const import (
19  ATTR_FLAP_ID,
20  ATTR_LOCATION,
21  ATTR_LOCK_STATE,
22  ATTR_PET_NAME,
23  DOMAIN,
24  SERVICE_SET_LOCK_STATE,
25  SERVICE_SET_PET_LOCATION,
26 )
27 from .coordinator import SurePetcareDataCoordinator
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 PLATFORMS = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR]
32 SCAN_INTERVAL = timedelta(minutes=3)
33 
34 
35 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
36  """Set up Sure Petcare from a config entry."""
37  hass.data.setdefault(DOMAIN, {})
38 
39  try:
40  hass.data[DOMAIN][entry.entry_id] = coordinator = SurePetcareDataCoordinator(
41  entry,
42  hass,
43  )
44  except SurePetcareAuthenticationError as error:
45  _LOGGER.error("Unable to connect to surepetcare.io: Wrong credentials!")
46  raise ConfigEntryAuthFailed from error
47  except SurePetcareError as error:
48  raise ConfigEntryNotReady from error
49 
50  await coordinator.async_config_entry_first_refresh()
51 
52  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
53 
54  lock_state_service_schema = vol.Schema(
55  {
56  vol.Required(ATTR_FLAP_ID): vol.All(
57  cv.positive_int, vol.In(coordinator.data.keys())
58  ),
59  vol.Required(ATTR_LOCK_STATE): vol.All(
60  cv.string,
61  vol.Lower,
62  vol.In(coordinator.lock_states_callbacks.keys()),
63  ),
64  }
65  )
66  hass.services.async_register(
67  DOMAIN,
68  SERVICE_SET_LOCK_STATE,
69  coordinator.handle_set_lock_state,
70  schema=lock_state_service_schema,
71  )
72 
73  set_pet_location_schema = vol.Schema(
74  {
75  vol.Required(ATTR_PET_NAME): vol.In(coordinator.get_pets().keys()),
76  vol.Required(ATTR_LOCATION): vol.In(
77  [
78  Location.INSIDE.name.title(),
79  Location.OUTSIDE.name.title(),
80  ]
81  ),
82  }
83  )
84  hass.services.async_register(
85  DOMAIN,
86  SERVICE_SET_PET_LOCATION,
87  coordinator.handle_set_pet_location,
88  schema=set_pet_location_schema,
89  )
90 
91  return True
92 
93 
94 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
95  """Unload a config entry."""
96  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
97  if unload_ok:
98  hass.data[DOMAIN].pop(entry.entry_id)
99 
100  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:35
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:94