original_docstring: Set up SimpliSafe as config entry.
docstring: Set up SimpliSafe as config entry.
docstring_tokens: Set up SimpliSafe as config entry .
code: async def async_setup_entry(hass, config_entry):
      """Set up SimpliSafe as config entry."""
      _verify_domain_control = verify_domain_control(hass, DOMAIN)
  
      websession = aiohttp_client.async_get_clientsession(hass)
  
      try:
          api = await API.login_via_token(config_entry.data[CONF_TOKEN], websession)
      except InvalidCredentialsError:
          _LOGGER.error("Invalid credentials provided")
          return False
      except SimplipyError as err:
          _LOGGER.error("Config entry failed: %s", err)
          raise ConfigEntryNotReady
  
      _async_save_refresh_token(hass, config_entry, api.refresh_token)
  
      simplisafe = SimpliSafe(hass, api, config_entry)
      await simplisafe.async_init()
      hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = simplisafe
  
      for component in ("alarm_control_panel", "lock"):
          hass.async_create_task(
              hass.config_entries.async_forward_entry_setup(config_entry, component)
          )
  
      @callback
      def verify_system_exists(coro):
          """Log an error if a service call uses an invalid system ID."""
  
          async def decorator(call):
              """Decorate."""
              system_id = int(call.data[ATTR_SYSTEM_ID])
              if system_id not in simplisafe.systems:
                  _LOGGER.error("Unknown system ID in service call: %s", system_id)
                  return
              await coro(call)
  
          return decorator
  
      @callback
      def v3_only(coro):
          """Log an error if the decorated coroutine is called with a v2 system."""
  
          async def decorator(call):
              """Decorate."""
              system = simplisafe.systems[int(call.data[ATTR_SYSTEM_ID])]
              if system.version != 3:
                  _LOGGER.error("Service only available on V3 systems")
                  return
              await coro(call)
  
          return decorator
  
      @verify_system_exists
      @_verify_domain_control
      async def remove_pin(call):
          """Remove a PIN."""
          system = simplisafe.systems[call.data[ATTR_SYSTEM_ID]]
          try:
              await system.remove_pin(call.data[ATTR_PIN_LABEL_OR_VALUE])
          except SimplipyError as err:
              _LOGGER.error("Error during service call: %s", err)
              return
  
      @verify_system_exists
      @_verify_domain_control
      async def set_pin(call):
          """Set a PIN."""
          system = simplisafe.systems[call.data[ATTR_SYSTEM_ID]]
          try:
              await system.set_pin(call.data[ATTR_PIN_LABEL], call.data[ATTR_PIN_VALUE])
          except SimplipyError as err:
              _LOGGER.error("Error during service call: %s", err)
              return
  
      @verify_system_exists
      @v3_only
      @_verify_domain_control
      async def set_system_properties(call):
          """Set one or more system parameters."""
          system = simplisafe.systems[call.data[ATTR_SYSTEM_ID]]
          try:
              await system.set_properties(
                  {
                      prop: value
                      for prop, value in call.data.items()
                      if prop != ATTR_SYSTEM_ID
                  }
              )
          except SimplipyError as err:
              _LOGGER.error("Error during service call: %s", err)
              return
  
      for service, method, schema in [
          ("remove_pin", remove_pin, SERVICE_REMOVE_PIN_SCHEMA),
          ("set_pin", set_pin, SERVICE_SET_PIN_SCHEMA),
          (
              "set_system_properties",
              set_system_properties,
              SERVICE_SET_SYSTEM_PROPERTIES_SCHEMA,
          ),
      ]:
          async_register_admin_service(hass, DOMAIN, service, method, schema=schema)
  
      return True
code_tokens: async def async_setup_entry ( hass , config_entry ) : _verify_domain_control = verify_domain_control ( hass , DOMAIN ) websession = aiohttp_client . async_get_clientsession ( hass ) try : api = await API . login_via_token ( config_entry . data [ CONF_TOKEN ] , websession ) except InvalidCredentialsError : _LOGGER . error ( "Invalid credentials provided" ) return False except SimplipyError as err : _LOGGER . error ( "Config entry failed: %s" , err ) raise ConfigEntryNotReady _async_save_refresh_token ( hass , config_entry , api . refresh_token ) simplisafe = SimpliSafe ( hass , api , config_entry ) await simplisafe . async_init ( ) hass . data [ DOMAIN ] [ DATA_CLIENT ] [ config_entry . entry_id ] = simplisafe for component in ( "alarm_control_panel" , "lock" ) : hass . async_create_task ( hass . config_entries . async_forward_entry_setup ( config_entry , component ) ) @ callback def verify_system_exists ( coro ) : """Log an error if a service call uses an invalid system ID.""" async def decorator ( call ) : """Decorate.""" system_id = int ( call . data [ ATTR_SYSTEM_ID ] ) if system_id not in simplisafe . systems : _LOGGER . error ( "Unknown system ID in service call: %s" , system_id ) return await coro ( call ) return decorator @ callback def v3_only ( coro ) : """Log an error if the decorated coroutine is called with a v2 system.""" async def decorator ( call ) : """Decorate.""" system = simplisafe . systems [ int ( call . data [ ATTR_SYSTEM_ID ] ) ] if system . version != 3 : _LOGGER . error ( "Service only available on V3 systems" ) return await coro ( call ) return decorator @ verify_system_exists @ _verify_domain_control async def remove_pin ( call ) : """Remove a PIN.""" system = simplisafe . systems [ call . data [ ATTR_SYSTEM_ID ] ] try : await system . remove_pin ( call . data [ ATTR_PIN_LABEL_OR_VALUE ] ) except SimplipyError as err : _LOGGER . error ( "Error during service call: %s" , err ) return @ verify_system_exists @ _verify_domain_control async def set_pin ( call ) : """Set a PIN.""" system = simplisafe . systems [ call . data [ ATTR_SYSTEM_ID ] ] try : await system . set_pin ( call . data [ ATTR_PIN_LABEL ] , call . data [ ATTR_PIN_VALUE ] ) except SimplipyError as err : _LOGGER . error ( "Error during service call: %s" , err ) return @ verify_system_exists @ v3_only @ _verify_domain_control async def set_system_properties ( call ) : """Set one or more system parameters.""" system = simplisafe . systems [ call . data [ ATTR_SYSTEM_ID ] ] try : await system . set_properties ( { prop : value for prop , value in call . data . items ( ) if prop != ATTR_SYSTEM_ID } ) except SimplipyError as err : _LOGGER . error ( "Error during service call: %s" , err ) return for service , method , schema in [ ( "remove_pin" , remove_pin , SERVICE_REMOVE_PIN_SCHEMA ) , ( "set_pin" , set_pin , SERVICE_SET_PIN_SCHEMA ) , ( "set_system_properties" , set_system_properties , SERVICE_SET_SYSTEM_PROPERTIES_SCHEMA , ) , ] : async_register_admin_service ( hass , DOMAIN , service , method , schema = schema ) return True
short_docstring: Set up SimpliSafe as config entry.
short_docstring_tokens: Set up SimpliSafe as config entry .
comment: """Set up SimpliSafe as config entry."""$SEP$"""Log an error if a service call uses an invalid system ID."""$SEP$"""Decorate."""$SEP$"""Log an error if the decorated coroutine is called with a v2 system."""$SEP$"""Decorate."""$SEP$"""Remove a PIN."""$SEP$"""Set a PIN."""$SEP$"""Set one or more system parameters."""