Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Agent."""
2 
3 from agent import AgentError
4 from agent.a import Agent
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import Platform
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryNotReady
10 from homeassistant.helpers import device_registry as dr
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 
13 from .const import DOMAIN as AGENT_DOMAIN, SERVER_URL
14 
15 ATTRIBUTION = "ispyconnect.com"
16 DEFAULT_BRAND = "Agent DVR by ispyconnect.com"
17 
18 PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.CAMERA]
19 
20 AgentDVRConfigEntry = ConfigEntry[Agent]
21 
22 
24  hass: HomeAssistant, config_entry: AgentDVRConfigEntry
25 ) -> bool:
26  """Set up the Agent component."""
27  server_origin = config_entry.data[SERVER_URL]
28 
29  agent_client = Agent(server_origin, async_get_clientsession(hass))
30  try:
31  await agent_client.update()
32  except AgentError as err:
33  await agent_client.close()
34  raise ConfigEntryNotReady from err
35 
36  if not agent_client.is_available:
37  raise ConfigEntryNotReady
38 
39  config_entry.async_on_unload(agent_client.close)
40 
41  await agent_client.get_devices()
42 
43  config_entry.runtime_data = agent_client
44 
45  device_registry = dr.async_get(hass)
46 
47  device_registry.async_get_or_create(
48  config_entry_id=config_entry.entry_id,
49  identifiers={(AGENT_DOMAIN, agent_client.unique)},
50  manufacturer="iSpyConnect",
51  name=f"Agent {agent_client.name}",
52  model="Agent DVR",
53  sw_version=agent_client.version,
54  )
55 
56  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
57 
58  return True
59 
60 
62  hass: HomeAssistant, config_entry: AgentDVRConfigEntry
63 ) -> bool:
64  """Unload a config entry."""
65  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, AgentDVRConfigEntry config_entry)
Definition: __init__.py:63
bool async_setup_entry(HomeAssistant hass, AgentDVRConfigEntry config_entry)
Definition: __init__.py:25
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)