<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def add_query(self, name, filter, **kwargs):
 """Add a new query to device query service. .. code-block:: python f = api.add_query( name = "Query name", filter = { "device_id": {"$eq": "01234"}, custom_attributes = { "foo": {"$eq": "bar"} } } ) print(f.created_at) :param str name: Name of query (Required) :param dict filter: Filter properties to apply (Required) :param return: The newly created query object. :return: the newly created query object :rtype: Query """

        # Ensure we have the correct types and get the new query object
        filter_obj = filters.legacy_filter_formatter(
            dict(filter=filter),
            Device._get_attributes_map()
        ) if filter else None
        query_map = Query._create_request_map(kwargs)
        # Create the DeviceQuery object
        f = DeviceQuery(name=name, query=filter_obj['filter'], **query_map)
        api = self._get_api(device_directory.DefaultApi)
        return Query(api.device_query_create(f))