Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for QVR Pro NVR software by QNAP."""
2 
3 import logging
4 
5 from pyqvrpro import Client
6 from pyqvrpro.client import AuthenticationError, InsufficientPermissionsError
7 from requests.exceptions import ConnectionError as RequestsConnectionError
8 import voluptuous as vol
9 
10 from homeassistant.const import (
11  CONF_HOST,
12  CONF_PASSWORD,
13  CONF_PORT,
14  CONF_USERNAME,
15  Platform,
16 )
17 from homeassistant.core import HomeAssistant, ServiceCall
19 from homeassistant.helpers.discovery import load_platform
20 from homeassistant.helpers.typing import ConfigType
21 
22 from .const import (
23  CONF_EXCLUDE_CHANNELS,
24  DOMAIN,
25  SERVICE_START_RECORD,
26  SERVICE_STOP_RECORD,
27 )
28 
29 DEFAULT_PORT = 8080
30 
31 SERVICE_CHANNEL_GUID = "guid"
32 
33 _LOGGER = logging.getLogger(__name__)
34 
35 CONFIG_SCHEMA = vol.Schema(
36  {
37  DOMAIN: vol.Schema(
38  {
39  vol.Required(CONF_HOST): cv.string,
40  vol.Required(CONF_USERNAME): cv.string,
41  vol.Required(CONF_PASSWORD): cv.string,
42  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
43  vol.Optional(CONF_EXCLUDE_CHANNELS, default=[]): vol.All(
44  cv.ensure_list_csv, [cv.positive_int]
45  ),
46  }
47  )
48  },
49  extra=vol.ALLOW_EXTRA,
50 )
51 
52 SERVICE_CHANNEL_RECORD_SCHEMA = vol.Schema(
53  {vol.Required(SERVICE_CHANNEL_GUID): cv.string}
54 )
55 
56 
57 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
58  """Set up the QVR Pro component."""
59  conf = config[DOMAIN]
60  user = conf[CONF_USERNAME]
61  password = conf[CONF_PASSWORD]
62  host = conf[CONF_HOST]
63  port = conf[CONF_PORT]
64  excluded_channels = conf[CONF_EXCLUDE_CHANNELS]
65 
66  try:
67  qvrpro = Client(user, password, host, port=port)
68 
69  channel_resp = qvrpro.get_channel_list()
70 
71  except InsufficientPermissionsError:
72  _LOGGER.error("User must have Surveillance Management permission")
73  return False
74  except AuthenticationError:
75  _LOGGER.error("Authentication failed")
76  return False
77  except RequestsConnectionError:
78  _LOGGER.error("Error connecting to QVR server")
79  return False
80 
81  channels = []
82 
83  for channel in channel_resp["channels"]:
84  if channel["channel_index"] + 1 in excluded_channels:
85  continue
86 
87  channels.append(channel)
88 
89  hass.data[DOMAIN] = {"channels": channels, "client": qvrpro}
90 
91  load_platform(hass, Platform.CAMERA, DOMAIN, {}, config)
92 
93  # Register services
94  def handle_start_record(call: ServiceCall) -> None:
95  guid = call.data[SERVICE_CHANNEL_GUID]
96  qvrpro.start_recording(guid)
97 
98  def handle_stop_record(call: ServiceCall) -> None:
99  guid = call.data[SERVICE_CHANNEL_GUID]
100  qvrpro.stop_recording(guid)
101 
102  hass.services.register(
103  DOMAIN,
104  SERVICE_START_RECORD,
105  handle_start_record,
106  schema=SERVICE_CHANNEL_RECORD_SCHEMA,
107  )
108  hass.services.register(
109  DOMAIN,
110  SERVICE_STOP_RECORD,
111  handle_stop_record,
112  schema=SERVICE_CHANNEL_RECORD_SCHEMA,
113  )
114 
115  return True
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:57
None load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:137